Advertisement
Guest User

FLS Practice Task 1

a guest
Sep 20th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void count_sort(int a[], int n)              //n == length of array a
  4. {
  5.     int min = INT_MAX, max = INT_MIN;
  6.     for (int i = 0; i < n; i++)
  7.     {
  8.         if (a[i] < min)
  9.             min = a[i];
  10.         if (a[i] > max)
  11.             max = a[i];
  12.     }
  13.     int *c = new int[max - min + 1];
  14.     for (int i = 0; i < max - min + 1; i++)
  15.         c[i] = 0;
  16.     for (int i = 0; i < n; i++)
  17.         c[a[i] - min]++;
  18.     int i = 0;
  19.     for (int j = min; j < max + 1; j++)
  20.         while (c[j - min] != 0)
  21.         {
  22.             a[i] = j;
  23.             c[j - min]--;
  24.             i++;
  25.         }
  26. }
  27.  
  28.  
  29. //implementation
  30. /*
  31. int main()
  32. {
  33.     int n;
  34.     std::cin >> n;
  35.     std::string *a = new std::string[n];
  36.    
  37.     for (int i = 0; i < n; i++)
  38.         std::cin >> a[i];
  39.     int *arr = new int[n];
  40.     for (int i = 0; i < n; i++)
  41.         arr[i] = stoi(a[i]);
  42.    
  43.     count_sort(arr, n);
  44.    
  45.     for (int i = 0; i < n; i++)
  46.         std::cout << arr[i] << " ";
  47.    
  48.     return 0;
  49. }
  50. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement