Advertisement
IISergeyII

Untitled

May 19th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <vector>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. const int N = 6;
  11.  
  12. void mySwap(int &a, int &b) {
  13.     int t = a;
  14.     a = b;
  15.     b = t;
  16. }
  17.  
  18. void printArray(int arr[]) {
  19.     for (int i = 0; i < N; i++) {
  20.         cout << arr[i] << " ";
  21.     }
  22.     cout << endl;
  23. }
  24.  
  25. void quickSort(int A[],int left, int rigth) {
  26.     int i = left, j = rigth;
  27.     int pivot = A[(rigth + left)/2];
  28.  
  29.     while (i <= j) {
  30.         while (A[i] < pivot) i++;
  31.         while (A[j] > pivot) j--;
  32.  
  33.         if (i <= j) {
  34.             mySwap(A[i], A[j]);
  35.             i++;
  36.             j--;
  37.         }
  38.     }
  39.     printArray(A);
  40.     if (left < j) quickSort(A, left, j);
  41.     if (rigth > i) quickSort(A, i, rigth);
  42. }
  43.  
  44. int main()
  45. {
  46.     int a[N] = {7, 9, 5, 1, 3, 0};
  47.  
  48.     quickSort(a, 0, N-1);
  49.  
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement