Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I can't tell you what it really is,
- // I can only tell you what it feels like.
- #include "bits/stdc++.h"
- using namespace std;
- int a[10000];
- int iter, n;
- int partition(int l, int r) {
- // cout << "before: ";
- // for (int i = l; i <= r; ++i) {
- // cout << a[i] << ' ';
- // }cout << endl;
- // cout << "pivot: " << a[l] << '\n';
- // cout << l << ' ' << r << ' ';
- // ++iter;
- // if (iter>100)exit(0);
- int pivot = a[l];
- int i = l+1;
- int j = r;
- while (i < j) {
- while (i < r && a[i] < pivot) ++i;
- while (j > l && a[j] >= pivot) --j;
- if (i < j) swap(a[i], a[j]);
- }
- swap(a[l], a[j]);
- // cout << "partition: " << j << '\n';
- // cout << "after: ";
- // for (int i = l; i <= r; ++i) {
- // cout << a[i] << ' ';
- // }cout << endl;
- // cout << endl;
- return j;
- }
- void quick(int lo, int hi) {
- if (lo >= hi) return;
- if (lo + 1 == hi) {
- if (a[lo] > a[hi]) swap(a[lo], a[hi]);
- return;
- }
- int p = partition(lo, hi);
- quick(lo, p-1);
- quick(p+1, hi);
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- srand(time(NULL));
- // n = rand() % 1000 + 1;
- cin >> n;
- for (int i = 0; i < n; ++i)
- cin >> a[i];
- // a[i] = rand() % 10000 + 7;
- random_shuffle(a, a+n);
- quick(0, n-1);
- // cout << n << '\n';
- for (int i = 0; i < n; ++i) {
- cout << a[i] << ' ';
- }
- // cout << endl;
- // if(!is_sorted(a, a+n)) cout << "fuck";
- assert(is_sorted(a, a+n));
- return 0;
- }
- // 1). Try forming/rearranging the equations if any
- // 2). If problem asks to count, try counting in reverse(it can be easier sometimes)
Add Comment
Please, Sign In to add comment