No, it doesn't work because you can't return a local variable.
The other issue is that your algorithm makes some numerical mistakes.
(1)
On line 47, you are dividing by
currentDigit, when you should be dividing by 10**
currentDigit. Use the
std::pow() function to calculate ten to a power (
#include <cmath>).
Once you fix that, you'll also have to adjust your indexing: 10
1 is 10, so you want
currentDigit to start at 0. You'll also want to change the condition on line 44 to strictly less-than.
(2)
The next problem is how you are peeling numbers out of your bins. Your counter loop on line 51 causes
i to remain the
same all during the inner loops (lines 52 through 57). Get rid of line 51's loop and just set
int i = 0;
. Don't forget to increment
i somewhere around line 55.
(3)
Okay, this is just to reiterate. Get rid of all the
a[i]'s and replace them with
q[i]'s, and get rid of line 42.
(4)
Your sort algorithm assumes that there will be 1000 numbers in your array. It also doesn't actually have to return a value. I recommend you change its prototype to:
void radixSort(int q[], int n)
Then you can call it in
main() this way:
1 2 3 4 5 6 7 8 9 10 11
|
int main() {
const int MAX=1000;
int data[MAX];
...
radixSort(data,MAX);
...
for (int j=0;j<MAX;j++)
cout << data[j] << " ";
...
|
By the way, I'm impressed you used an array of ten
std::queue for your base-10 radix sort. Very nice!
Hope this helps.