Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. void countSort(int* array, int n)
  2. {
  3.     long long unsigned int C = 0, M = 0;
  4.     int max = -2147483647;
  5.     int min = 2147483647;
  6.     for (int i = 0; i < n; i++) {
  7.         if (array[i] < min) min = array[i];
  8.         if (array[i] > max) max = array[i];
  9.     }
  10.     int range = max - min + 1;
  11.  
  12.     int* count_array = new int[range];
  13.     int* output_array = new int[n];
  14.  
  15.     for (int i = 0; i < range; i++) {
  16.         count_array[i] = 0;
  17.     }
  18.     for (int i = 0; i < n; i++) {
  19.         output_array[i] = 0;
  20.     }
  21.     for (int i = 0; i < n; i++) {
  22.         count_array[array[i] - min]++;
  23.         C++;
  24.     }
  25.     for (int i = 1; i < range; i++) {
  26.         count_array[i] += count_array[i - 1];
  27.         C++;
  28.     }
  29.     for (int i = n - 1; i >= 0; i--)
  30.     {
  31.         output_array[count_array[array[i] - min] - 1] = array[i];
  32.         count_array[array[i] - min]--;
  33.         M++;
  34.     }
  35.  
  36.     for (int i = 0; i < n; i++)
  37.         array[i] = output_array[i];
  38.     //delete[] count_array;
  39.     //delete[] output_array;
  40.     if (n != 10) cout << "C+M = " << C + M;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement