Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. int qs_find_pivot(Student* a, int i, int j)
  2. {
  3.     for (int t = i + 1; t <= j; t++)
  4.     {
  5.         if (a[i] < a[t]) return t;
  6.         if (a[i] > a[t]) return i;
  7.     }
  8.     return -1;
  9. }
  10.  
  11. int qs_partition(Student* a, int i, int j, int ip)
  12. {
  13.     int l = i, r = j;
  14.     Student t = a[ip];
  15.  
  16.     do
  17.     {
  18.         while (a[l] < t) l++;
  19.         while (a[r] >= t) r--;
  20.         if (l < r) swap(a[r], a[l]);
  21.     } while (l <= r);
  22.     return l;
  23. }
  24.  
  25. void QuickSort(Student * a, int n)
  26. {
  27.     int ip, k, i, j;
  28.     queue<int> s;
  29.     s.push(0);
  30.     s.push(n - 1);
  31.  
  32.     while (!s.empty())
  33.     {
  34.         i = s.front();
  35.         s.pop();
  36.         j = s.front();
  37.         s.pop();
  38.         ip = qs_find_pivot(a, i, j);
  39.         if (ip < 0) continue;
  40.  
  41.         k = qs_partition(a, i, j, ip);
  42.         s.push(i);
  43.         s.push(k - 1);
  44.         s.push(k);
  45.         s.push(j);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement