Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. /*
  2.     Algorithms and Data Structures
  3.     Spring 2019
  4.     Assignment #6
  5.     @file RadixSort.cpp
  6.     @author Taiyr Begeyev
  7.     @version 1.0 25/03/19
  8. */
  9.  
  10. /*
  11. __________           .__                  
  12. \__    ___/ _____    |__|  ___.__. _______
  13.   |    |    \__  \   |  | <   |  | \_  __ \
  14.   |    |     / __ \_ |  |  \___  |  |  | \/
  15.   |____|    (____  / |__|  / ____|  |__|  
  16.                 \/        \/              
  17. */
  18. #include <iostream>
  19. #include <algorithm>
  20. using namespace std;
  21.  
  22. void CountingSort(int*, int, int);
  23. void RadixSort(int*, int);
  24. void printArr(int*, int);
  25.  
  26. int howManyDigits(int n) {
  27.     int counter = 0;
  28.     int hey;
  29.     while(n > 0) {
  30.         hey = n % 10;
  31.         n /= 10;
  32.         counter++;
  33.     }
  34.     return counter;
  35. }
  36.  
  37. int main() {
  38.     int n;
  39.     cout << "Enter the number of elements: ";
  40.     cin >> n;
  41.     int *sequence = new int[n + 1];
  42.     cout << "Enter the elements: ";
  43.  
  44.     for (int i = 0; i < n; i++)
  45.         cin >> sequence[i];
  46.  
  47.     RadixSort(sequence, n);
  48.     cout << "Sorted Array: " << endl;
  49.     printArr(sequence, n);
  50.  
  51.     delete[] sequence;
  52.  
  53.     return 0;
  54. }
  55.  
  56. /**
  57.  * @brief Counting Sort Algorithm Implementation
  58.  *
  59.  * Algorithm assumes that each of the input elements
  60.  * is an integer in the range 0 to k, for some k. It
  61.  * determines the number of elements less than x, for
  62.  * each input element x. It uses the information to place
  63.  * element x directly into its position in the output array
  64.  *
  65.  * @param A - array to be sorted
  66.  * @param n - number of elements in the array
  67. */
  68.  
  69. void CountingSort(int *A, int n, int exp) {
  70.     if (exp == 0)
  71.         exp = 1;
  72.     // create Auxiliary Array
  73.     int max = *max_element(A, A + n);
  74.     //int min = *min_element(A, A + n);
  75.     //int range = max - min + 1;
  76.     int *C = new int[max];
  77.     // create the resultant array
  78.     int *B = new int[n];
  79.  
  80.     // initialize the array with zeros
  81.     for (int i = 0; i < max; i++) {
  82.         C[i] = 0;
  83.     }
  84.     // iterate through array, calculate and the number
  85.     // of occurences of A[i] to C[A[i]]
  86.     for (int i = 0; i < n; i++) {
  87.         C[( A[i] / exp )]++;
  88.     }
  89.  
  90.     // determine for each i = 0 .. k how many input elements
  91.     // are less than or equal to i
  92.     for (int i = 1; i < max; i++) {
  93.         C[i] += C[i - 1];
  94.     }
  95.  
  96.     // place each element from C into its correct sorted position
  97.     for (int i = n - 1; i >= 0; i--) {
  98.         B[C[(A[i] / exp)] - 1] = A[i];
  99.         C[(A[i] / exp)]--;
  100.     }
  101.     for (int i = 0; i < n; i++) {
  102.         A[i] = B[i];
  103.     }
  104.    
  105.     // deallocate
  106.     delete [] B;
  107.     delete [] C;
  108. }
  109.  
  110. void RadixSort(int *arr, int n) {
  111.     int maxDigit = 0;
  112.  
  113.     for (int i = 0; i < n; i++) {
  114.         if (howManyDigits(arr[i]) > maxDigit)
  115.             maxDigit = howManyDigits(arr[i]);
  116.     }
  117.     int exp = 1;
  118.     for (int i = 0; i < maxDigit - 1; i++) {
  119.         exp *= 10;
  120.     }
  121.     for (int i = 0; i < n; i++) {
  122.         CountingSort(arr, n, exp);
  123.         exp /= 10;
  124.     }
  125. }
  126.  
  127. /**
  128.  * @brief print the passed Array
  129.  * @param array - to be printed
  130.  * @param n - number of elements
  131. */
  132.  
  133. void printArr(int *arr, int n) {
  134.     for (int i = 0; i < n; i++)
  135.         cout << arr[i] << " ";
  136.     cout << endl;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement