Page 232 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 232

Unit 13: Sorting




                          int bucket[10] ={0};                                                  Notes
                          for (i = 0; i < n; i++)
                                  bucket[a[i] / exp % 10]++;
                          for (i = 1; i < 10; i++)
                                  bucket[i] += bucket[i - 1];
                          for (i = n - 1; i >= 0; i—)
                                  b[—bucket[a[i] / exp % 10]] = a[i];
                          for (i = 0; i < n; i++)
                                  a[i] = b[i];
                          exp *= 10;
                          cout << “nPASS   :”;
                          print(a, n);    //Prints the results from the first pass
                   }
          }
            int main()
          {
                  int arr[MAX];
                  int i, n;
                  cout << “Enter total elements n <” << MAX << endl;
                  cin>>n;
                  cout << “Enter” << n << “ Elements :” << endl;
                  for (i = 0; i < n; i++)
                          cin>>arr[i];
                   cout<<“nARRAY  : “;
                  print(&arr[0], n);
                  radixsort(&arr[0], n);
                  cout << “nSORTED : “;
                  print(&arr[0], n);
                  printf(“n”);
                  return 0;
          }
          The output is shown as below:






















                                           LOVELY PROFESSIONAL UNIVERSITY                                   225
   227   228   229   230   231   232   233   234   235   236   237