allia

слишком медленный хоаr

Sep 21st, 2020 (edited)
1,879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. void hoarasort(int *arr, int left, int right)
  7. {
  8.  
  9. int i = left, j = right, x = 0;
  10. int key = arr[(left + right) / 2];
  11.  
  12. do {
  13.    while (arr[i] < key)
  14.      i++;
  15.    while (arr[j] > key)
  16.      j--;
  17.  
  18.    if (i <= j)
  19.    {
  20.      if (i < j)
  21.      {
  22.       x = arr[i];
  23.       arr[i] = arr[j];
  24.       arr[j] = x;
  25.      }
  26.      i++;
  27.      j--;
  28.    }
  29. } while (i <= j);
  30.  
  31. if (i < right)
  32.    hoarasort(arr, i, right);
  33. if (left < j)
  34.    hoarasort(arr, left, j);
  35. }
  36.  
  37. int main ()
  38. {
  39.   int n = 0;
  40.   cin >> n;
  41.   int *arr;
  42.   arr = new int [n];
  43.  
  44.   for (int i=0; i<n; i++)
  45.   cin >> arr[i];
  46.  
  47.   int right = n-1;
  48.   int left = 0;
  49.  
  50.   hoarasort(arr, left, right);
  51.  
  52.   for (int i=0; i<n; i++)
  53.   cout << arr[i] << " ";
  54. }
Add Comment
Please, Sign In to add comment