#include <bits/stdc++.h> using namespace std; void radixSort(int *a, int arraySize){ vector<int> mybucket[10]; int i,j,maxVal = 0,digitPosition=1; for(i = 0; i < arraySize; i++){ if(a[i] > maxVal) maxVal = a[i]; } int p =1; char str[20]; sprintf( str,"%d",maxVal); while( p <= strlen(str)){ for(i = 0; i < arraySize; i++) mybucket[(a[i]/digitPosition)%10].push_back(a[i]); int k = 0; cout<<"\nIteration"<<p<<"\n"; for( i = 9;i>=0;i--){ cout<<"Bucket"<<i<<"["; for(j=0;j<mybucket[i].size();j++){ a[k++] = mybucket[i][j]; cout<<a[k-1]<<","; } cout<<"]\n"; mybucket[i].clear(); } digitPosition *= 10; p+=1; } } int main() { int *a = new int[20]; cout<<"I/P\n[ "; for( int i =0;i<20;i++){ a[i] = random()%100; cout<<a[i]<<","; } cout<<"]\n"; radixSort(a,20); cout<<"\nO/P\n[ "; for(int i=0;i<20;i++){ cout<<a[i]<<","; } cout<<"]"; return 0; }
Standard input is empty
I/P [ 83,86,77,15,93,35,86,92,49,21,62,27,90,59,63,26,40,26,72,36,] Iteration1 Bucket9[49,59,] Bucket8[] Bucket7[77,27,] Bucket6[86,86,26,26,36,] Bucket5[15,35,] Bucket4[] Bucket3[83,93,63,] Bucket2[92,62,72,] Bucket1[21,] Bucket0[90,40,] Iteration2 Bucket9[93,92,90,] Bucket8[86,86,83,] Bucket7[77,72,] Bucket6[63,62,] Bucket5[59,] Bucket4[49,40,] Bucket3[36,35,] Bucket2[27,26,26,21,] Bucket1[15,] Bucket0[] O/P [ 93,92,90,86,86,83,77,72,63,62,59,49,40,36,35,27,26,26,21,15,]