Advertisement
double_trouble

qsort

Mar 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9.  
  10. using namespace std;
  11.  
  12. int a[5000010];
  13.  
  14. void qsort(int l, int r)
  15. {
  16.     int i = l, j = r;
  17.     srand(r);
  18.     long d = a[(rand() % (r - l + 1)) + l];
  19.     do
  20.     {
  21.         while (a[i]>d)
  22.             i++;
  23.         while (a[j]<d)
  24.             j--;
  25.         if (i <= j)
  26.         {
  27.             swap(a[i], a[j]);
  28.             i++;
  29.             j--;
  30.         }
  31.     } while (i <= j);
  32.     if (l < j)
  33.         qsort(l, j);
  34.     if (r > i)
  35.         qsort(i, r);
  36. }
  37.  
  38. int main()
  39. {
  40.     int n;
  41.     cin >> n;
  42.     for (int i = 0; i < n; i++)
  43.         cin >> a[i];
  44.  
  45.     qsort(0, n - 1);
  46.  
  47.  
  48.     for (int i = 0; i < n; i++)
  49.         cout << a[i] << " ";
  50.     //system("PAUSE");
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement