Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> a;
- void _swap(int &a, int &b)
- {
- printf("swapped %d & %d; ", a, b);
- swap(a, b);
- }
- void print()
- {
- cout << "\ncurrent state: ";
- for (auto x : a)
- cout << x << " ";
- cout << endl
- << endl;
- }
- int partition(int l, int h)
- {
- int pivot = a[l];
- int start = l;
- int end = h;
- printf("pivot %d -> ", pivot);
- while (start < end)
- {
- while (a[start] <= pivot)
- start++;
- while (a[end] > pivot)
- end--;
- if (start < end)
- {
- // printf("i & j: %d %d, ", start, end);
- _swap(a[start], a[end]);
- }
- }
- cout << " pivot ";
- _swap(a[l], a[end]);
- print();
- return end;
- }
- void quick_sort(int l, int h)
- {
- int loc;
- if (l < h)
- {
- loc = partition(l, h);
- quick_sort(l, loc - 1);
- quick_sort(loc + 1, h);
- }
- }
- int main()
- {
- int n;
- // cout << "enter size & elements of the array \n";
- cin >> n;
- a.resize(n);
- for (auto &x : a)
- cin >> x;
- quick_sort(0, n - 1);
- cout << "\n quick sorted array ";
- for (auto x : a)
- cout << x << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment