Advertisement
IISergeyII

Untitled

May 19th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 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 = 5;
  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.             printArray(A);
  31.         while (A[i] < pivot) i++;
  32.         while (A[j] > pivot) j--;
  33.  
  34.         if (i < j) {
  35.             mySwap(A[i], A[j]);
  36.             i++;
  37.             j--;
  38.         }
  39.  
  40.     }
  41.  
  42.     if (left < j) quickSort(A, left, j);
  43.     if (rigth > i) quickSort(A, i, rigth);
  44. }
  45.  
  46. int main()
  47. {
  48.     int a[N] = {7, 9, 5, 1, 3};
  49.  
  50.     quickSort(a, 0, N-1);
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement