Advertisement
Guest User

Radix

a guest
Apr 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. include<iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. // A utility function to get maximum value in arr[]
  8.  
  9. int getMax(int arr[], int n)
  10.  
  11. {
  12.  
  13.     int mx = arr[0];
  14.  
  15.     for (int i = 1; i < n; i++)
  16.  
  17.         if (arr[i] > mx)
  18.  
  19.             mx = arr[i];
  20.  
  21.     return mx;
  22.  
  23. }
  24.  
  25.  
  26.  
  27. // A function to do counting sort of arr[] according to
  28.  
  29. // the digit represented by exp.
  30.  
  31. void countSort(int arr[], int n, int exp)
  32.  
  33. {
  34.  
  35.     int output[n]; // output array
  36.  
  37.     int i, count[10] = {0};
  38.  
  39.  
  40.  
  41.     // Store count of occurrences in count[]
  42.  
  43.     for (i = 0; i < n; i++)
  44.  
  45.         count[ (arr[i]/exp)%10 ]++;
  46.  
  47.  
  48.  
  49.     // Change count[i] so that count[i] now contains actual
  50.  
  51.     //  position of this digit in output[]
  52.  
  53.     for (i = 1; i < 10; i++)
  54.  
  55.         count[i] += count[i - 1];
  56.  
  57.  
  58.  
  59.     // Build the output array
  60.  
  61.     for (i = n - 1; i >= 0; i--)
  62.  
  63.     {
  64.  
  65.         output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
  66.  
  67.         count[ (arr[i]/exp)%10 ]--;
  68.  
  69.     }
  70.  
  71.  
  72.  
  73.     // Copy the output array to arr[], so that arr[] now
  74.  
  75.     // contains sorted numbers according to current digit
  76.  
  77.     for (i = 0; i < n; i++)
  78.  
  79.         arr[i] = output[i];
  80.  
  81. }
  82.  
  83.  
  84.  
  85. // The main function to that sorts arr[] of size n using
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement