Radix Sort

closed account (S3TkoG1T)
I'm having trouble implementing my radix sort program on the main .cpp file.

Here is what the radix header file looks like::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <vector>
#include <queue>
using namespace std;

void distribute(const vector<int> &v, queue<int> digitQueue[], int pwr) {
	int i;
	for(int i=0; i < v.size(); i++)
	digitQueue[(v[i]/pwr) % 10].push(v[i]);
}

void collect(queue<int> digitQueue[], vector<int> &v) {
	int i = 0;
	
	for(int digit = 0; digit < 10; digit++)
		while(!digitQueue[digit].empty()) {
			v[i] = digitQueue[digit].front();
			digitQueue[digit].pop();
			i++;
			}
}

void radixSort(vector<int>& v, int d) { 
  
 	int power = 1; 
 	queue<int> digitQueue[10];

 	for (int i=0; i < d; i++) { 
  	distribute(v, digitQueue, power); 
  	collect(digitQueue, v); 
  	power *= 10; 
 	} 
}


Here is what my main looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>

#include "radix.h"

using namespace std;

int main() {

	
	vector<int> v(10);	
		
	radixSort(v, 10);
	
	cout << "sorted array is: " << endl;
	
	for(int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}


	cout << endl;	
return 0;
}


My output:

sorted array is:
0 0 0 0 0 0 0 0 0 0



I've days trying to figure out what I am doing wrong!

Thanks so much!
Last edited on
Why don't you try printing the contents of your vector before you actually sort them?

Maybe that'll give you some insight. :)
Topic archived. No new replies allowed.