Advertisement
Josif_tepe

Untitled

Sep 30th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. int counter;
  5. void quick_sort(std::vector<int> arr, int S, int E) {
  6.     int i = S, j = E;
  7.     int pivotal = arr[(S + E) / 2];
  8.     while(i <= j) {
  9.         while(arr[i] < pivotal) {
  10.             counter++;
  11.             i++;
  12.         }
  13.         while(arr[j] > pivotal) {
  14.             counter++;
  15.             j--;
  16.         }
  17.        
  18.         if(i <= j) {
  19.             counter++;
  20.             std::swap(arr[i], arr[j]);
  21.             i++;
  22.             j--;
  23.         }
  24.         counter++;
  25.     }
  26.     if(S < j) {
  27.         counter++;
  28.         quick_sort(arr, S, j);
  29.     }
  30.     if(i < E) {
  31.         counter++;
  32.         quick_sort(arr, i, E);
  33.     }
  34. }
  35.                                          
  36. int main()
  37. {
  38.     std::ios_base::sync_with_stdio(0);
  39.     // 1, 2, 3, 4, 5, 6, 7
  40.     // 5, 4, 3, 2, 1, 6, 7
  41.     std::vector<int> v{1, 2, 3, 4 ,5 ,6, 7};
  42.    
  43.     int maks=  0;
  44.     do{
  45.         counter = 0;
  46.         std::vector<int> v2 = v;
  47.         quick_sort(v2, 0, 6);
  48.         if(counter == 37) {
  49.             for(int j = 0; j < 7; j++) {
  50.                 std::cout << v[j] << " ";
  51.             }
  52.             std::cout << std::endl;
  53.         }
  54.        
  55.        
  56.       }while(std::next_permutation(v.begin(), v.end()));
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement