Advertisement
STANAANDREY

qsort cls

Oct 19th, 2020 (edited)
1,916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int partition(int a[], int l, int r) {
  5.     int pivot = a[r];
  6.     int i = l - 1;
  7.     for (int j = l; j < r; j++) {
  8.         if (a[j] <= pivot) {
  9.             i++;
  10.             swap(a[i], a[j]);
  11.         }
  12.     }
  13.     swap(a[i + 1], a[r]);
  14.     return i + 1;
  15. }
  16.  
  17. void quicksort(int a[], int l, int r) {
  18.     if (l < r) {
  19.         int pivot = partition(a, l, r);
  20.         quicksort(a, l, pivot - 1);
  21.         quicksort(a, pivot + 1, r);
  22.     }
  23. }
  24.  
  25. void read(int &n, int v[]) {
  26.     cin >> n;
  27.     for (int i = 0; i < n; i++)
  28.         cin >> v[i];
  29. }
  30.  
  31. int main () {
  32.     int n, v[1000];
  33.     read(n, v);
  34.     quicksort(v, 0, n - 1);
  35.     for (int i = 0; i < n; i++)
  36.         cout << v[i] << ' ';
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement