fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void radixSort(int *a, int arraySize){
  5. vector<int> mybucket[10];
  6. int i,j,maxVal = 0,digitPosition=1;
  7. for(i = 0; i < arraySize; i++){
  8. if(a[i] > maxVal)
  9. maxVal = a[i];
  10. }
  11. int p =1;
  12. char str[20];
  13. sprintf( str,"%d",maxVal);
  14.  
  15. while( p <= strlen(str)){
  16. for(i = 0; i < arraySize; i++)
  17. mybucket[(a[i]/digitPosition)%10].push_back(a[i]);
  18. int k = 0;
  19. cout<<"\nIteration"<<p<<"\n";
  20. for( i = 9;i>=0;i--){
  21. cout<<"Bucket"<<i<<"[";
  22. for(j=0;j<mybucket[i].size();j++){
  23. a[k++] = mybucket[i][j];
  24. cout<<a[k-1]<<",";
  25. }
  26. cout<<"]\n";
  27. mybucket[i].clear();
  28. }
  29. digitPosition *= 10;
  30. p+=1;
  31. }
  32. }
  33.  
  34. int main() {
  35. int *a = new int[20];
  36. cout<<"I/P\n[ ";
  37. for( int i =0;i<20;i++){
  38. a[i] = random()%100;
  39. cout<<a[i]<<",";
  40. }
  41. cout<<"]\n";
  42. radixSort(a,20);
  43. cout<<"\nO/P\n[ ";
  44. for(int i=0;i<20;i++){
  45. cout<<a[i]<<",";
  46. }
  47. cout<<"]";
  48. return 0;
  49. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
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,]