Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int qs_find_pivot(Student* a, int i, int j)
- {
- for (int t = i + 1; t <= j; t++)
- {
- if (a[i] < a[t]) return t;
- if (a[i] > a[t]) return i;
- }
- return -1;
- }
- int qs_partition(Student* a, int i, int j, int ip)
- {
- int l = i, r = j;
- Student t = a[ip];
- do
- {
- while (a[l] < t) l++;
- while (a[r] >= t) r--;
- if (l < r) swap(a[r], a[l]);
- } while (l <= r);
- return l;
- }
- void QuickSort(Student * a, int n)
- {
- int ip, k, i, j;
- queue<int> s;
- s.push(0);
- s.push(n - 1);
- while (!s.empty())
- {
- i = s.front();
- s.pop();
- j = s.front();
- s.pop();
- ip = qs_find_pivot(a, i, j);
- if (ip < 0) continue;
- k = qs_partition(a, i, j, ip);
- s.push(i);
- s.push(k - 1);
- s.push(k);
- s.push(j);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement