Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int partition(int a[], int l, int r) {
- int pivot = a[r];
- int i = l - 1;
- for (int j = l; j < r; j++) {
- if (a[j] <= pivot) {
- i++;
- swap(a[i], a[j]);
- }
- }
- swap(a[i + 1], a[r]);
- return i + 1;
- }
- void quicksort(int a[], int l, int r) {
- while (l < r) {
- int pivot = partition(a, l, r);
- if (pivot - l < r - pivot) {
- quicksort(a, l, pivot - 1);
- l = pivot + 1;
- } else {
- quicksort(a, pivot + 1, r);
- r = pivot - 1;
- }
- }
- }
- int a[] = {20, 3, 2, 1, 4, 0, 9, -1};
- int main () {
- quicksort(a, 1, 7);
- for (int i = 1; i <= 7; i++)
- std::cerr << a[i] << '\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment