Sc3ric

quicksort good

Feb 21st, 2023
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. int partition1(vector<vector<int>>& a, int ii, int start, int end, Efficiency* eff)
  2. {
  3.     int pivot = a[ii][end];
  4.     int i = start;
  5.     int j = start;
  6.     while (i < end) {
  7.         eff->comparisons++;
  8.         if (a[ii][i] < pivot) {
  9.             i++;
  10.         }
  11.         else {
  12.             if (i != j) {
  13.                 eff->swaps++;
  14.                 swap(&a[ii][i], &a[ii][j]);
  15.             }
  16.             i++;
  17.             j++;
  18.         }
  19.     }
  20.     if (i != j) {
  21.         eff->swaps++;
  22.         swap(&a[ii][i], &a[ii][j]);
  23.     }
  24.     i++;
  25.     j++;
  26.     return j - 1;
  27. }
  28.  
  29. void quick1(vector<vector<int>>& a, int ii, int start, int end, Efficiency* eff)
  30. {
  31.     if (start < end)
  32.     {
  33.         int p = partition1(a, ii, start, end, eff);
  34.         quick1(a, ii, start, p - 1, eff);
  35.         quick1(a, ii, p + 1, end, eff);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment