Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int partition(vector<int>& a, int start, int end, int& comparisons, int& swaps)
- {
- int pivot = a[end];
- int i = start;
- int j = start;
- while (i < end) {
- comparisons++;
- if (a[i] > pivot) {
- i++;
- }
- else {
- if (i != j) {
- swaps++;
- swap(&a[i], &a[j]);
- }
- i++;
- j++;
- }
- }
- if (i != j) {
- swaps++;
- swap(&a[i], &a[j]);
- }
- i++;
- j++;
- return j - 1;
- }
- void quickReq(vector<int>& a, int start, int end, int& comparisons, int& swaps)
- {
- if (start < end)
- {
- int p = partition(a, start, end, comparisons, swaps);
- quickReq(a, start, p - 1, comparisons, swaps);
- quickReq(a, p + 1, end, comparisons, swaps);
- }
- }
- vector<int> QuickSort(vector<int> a, int& comparisons, int& swaps) {
- int n = a.size();
- comparisons = 0;
- swaps = 0;
- quickReq(a, 0, n-1, comparisons, swaps);
- return a;
- }
Advertisement
Add Comment
Please, Sign In to add comment