c++ - Radix Sort using queues -
my program takes unsorted array , sorts in using radix sort. radix sort uses array of queues, 1 per possible digit , runs through each digit , places array until place values run through , array sorted. right sort doesn't run properly, spits out improper values, believe has how placing them array. appreciated, thanks.
#include <iostream> #include <queue> #include <ctime> #include <cstdlib> using namespace std; int *radixsort(int q[]); int main() { const int max=1000; int radix[max], *sortedradix; int seed =time(0); srand(seed); //fill array random #s (int i=0;i<max;i++){ int num=0+rand()%max; radix[i]=num; } //print unsorted (int j=0;j<max;j++) cout << radix[j] << " "; cout << endl << endl; //radix sort sortedradix=radixsort(radix); //print sorted (int j=0;j<max;j++) cout << sortedradix[j] << " "; cout << endl <<endl; cout<<"flag"; return 0; } int *radixsort(int q[]) { queue<int> bins[10]; //one array per possible digit int maxdigits=3; //holds amount of digits in largest number int currentdigit=1; //right digit int a[1000]; //holds numbers during sort while (currentdigit <= maxdigits) { for(int i=0; i<1000; i++){ //loop through whole array int num = q[i]; //set current value of array position int digitvalue = static_cast<int>(num/currentdigit)%10; //get decimal digit @ current digit bins[digitvalue].push(num); //put digits in corresponding bins } //transfer results of bins main array (int j=0; j<1000; j++){ for(int k=0;k<10;k++){ //loop through bins while (!bins[k].empty()){ //push elements in bin[k] until empty int temp=bins[k].front(); a[j]=temp; bins[k].pop(); } } } currentdigit++; } return a; }
Comments
Post a Comment