The program output is also shown below. /* * C++ Program To Implement Radix Sort */ #include…

The program output is also shown below. /* * C++ Program To Implement Radix Sort */ #include…

The program output is also shown below.

  1. /*
  2. * C++ Program To Implement Radix Sort
  3. */
  4. #include
  5. #include
  6. using namespace std;
  7. /*
  8. * get maximum value in arr[]
  9. */
  10. int getMax(int arr[], int n)
  11. {
  12. int max = arr[0];
  13. for (int i = 1; i
  14. if (arr[i] > max)
  15. max = arr[i];
  16. return max;
  17. }
  18. /*
  19. * count sort of arr[]
  20. */
  21. void countSort(int arr[], int n, int exp)
  22. {
  23. int output[n];
  24. int i, count[10] = {0};
  25. for (i = 0; i
  26. count[(arr[i] / exp) % 10]++;
  27. for (i = 1; i
  28. count[i] += count[i – 1];
  29. for (i = n – 1; i >= 0; i–)
  30. {
  31. output[count[(arr[i] / exp) % 10] – 1] = arr[i];
  32. count[(arr[i] / exp) % 10]–;
  33. }
  34. for (i = 0; i
  35. arr[i] = output[i];
  36. }
  37. /*
  38. * sorts arr[] of size n using Radix Sort
  39. */
  40. void radixsort(int arr[], int n)
  41. {
  42. int m = getMax(arr, n);
  43. for (int exp = 1; m / exp > 0; exp *= 10)
  44. countSort(arr, n, exp);
  45. }
  46. /*
  47. * Main
  48. */
  49. int main()
  50. {
  51. int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
  52. int n = sizeof(arr)/sizeof(arr[0]);
  53. radixsort(arr, n);
  54. for (int i = 0; i
  55. cout
  56. return 0;
  57. }