Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is wrong with my Radix Sort (written in C++)? I\'m trying to sort lists of

ID: 3687255 • Letter: W

Question

What is wrong with my Radix Sort (written in C++)?

I'm trying to sort lists of integers that are stored in an IntVector. IntVector is a custom vector class implemented in another file using a dynamic array

Here are the two function declarations for getting an int from IntVector:

/*
* Return integer value at given index
* Throw exception or abort with error message if index
* out of range.
*/
int IntVector::get(int index) const;

/*
* Set array element at given index to newVal.
*
* Throw exception or abort with error message if index
* out of range.
*/
void IntVector:: set(int index, int newVal);

Radix Sort (Least significant digit approach)

/ * Radix Sort
* Takes in as arguments a reference to an IntVector, and the size of the IntVector
*/

void radixSort(IntVector &data, int size)
{
   int bucket[10];
   int maxVal = maxValue(data);
   int decimalPlace = 1;
   int bucketIndex;

   while(maxVal / decimalPlace > 0) {

       int digitCount[10] = {0};

       for(int i = 0; i < size; i++) {
       digitCount[ (data.get(i) / decimalPlace) % 10 ]++;
       }

       for(int i = 1; i < 10; i++) {
       digitCount[i] += digitCount[i-1];
       }
       for(int i = size - 1; i >= 0; i--){
       bucketIndex = digitCount[(data.get(i) / decimalPlace) % 10];
       bucket[bucketIndex--] = data.get(i);
       }
       // Re-order the original array using elements in bucket
       for(int i = 0; i < size; i++) {
       data.set(i, bucket[i]);
       }
       // Move to the next decimal place
       decimalPlace *= 10;
   }
}

Any guidance would be much appreciated!

Explanation / Answer

#include using namespace std; class IntVector { int arr[50]; int size; public: IntVector(int s) { size=s; } int get(int index)const; void set(int index, int newVval); }; /* * Return integer value at given index * Throw exception or abort with error message if index * out of range. */ int IntVector::get(int index) const{ if(index>=0 && index=0 && indexmax) { max=data.get(i); } } return max; } //Radix Sort (Least significant digit approach) /* Radix Sort * Takes in as arguments a reference to an IntVector, and the size of the IntVector */ void radixSort(IntVector &data, int size) { int bucket[10]; int maxVal = maxValue(data,size); int decimalPlace = 1; int bucketIndex; while(maxVal / decimalPlace > 0) { int digitCount[10] = {0}; for(int i = 0; i < size; i++) { digitCount[ (data.get(i) / decimalPlace) % 10 ]++; } for(int i = 1; i < 10; i++) { digitCount[i] += digitCount[i-1]; } for(int i = size - 1; i >= 0; i--){ bucketIndex = digitCount[(data.get(i) / decimalPlace) % 10]; bucket[bucketIndex--] = data.get(i); } // Re-order the original array using elements in bucket for(int i = 0; i < size; i++) { data.set(i, bucket[i]); } // Move to the next decimal place decimalPlace *= 10; } cout<

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote