Sc3ric

quicksort

Feb 19th, 2023 (edited)
530
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 - 1);
  5.  
  6.  
  7.     for (int j = start; j < end; j++)
  8.     {
  9.         eff->comparisons++;
  10.         if (a[ii][j] > pivot)
  11.         {
  12.             i++;
  13.             if (i != j) {
  14.                 eff->swaps++;
  15.                 swap(&a[ii][i], &a[ii][j]);
  16.             }
  17.            
  18.         }
  19.     }
  20.  
  21.     if (i + 1 != end) {
  22.         eff->swaps++;
  23.         swap(&a[ii][i + 1], &a[ii][end]);
  24.     }
  25.    
  26.     return (i + 1);
  27. }
  28.  
  29. void quick1(vector<vector<int>>& a, int ii, int start, int end, Efficiency* eff)
  30. {
  31.     //eff->comparisons++;
  32.     if (start < end)
  33.     {
  34.         int p = partition1(a, ii, start, end, eff);
  35.         quick1(a, ii, start, p - 1, eff);
  36.         quick1(a, ii, p + 1, end, eff);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment