Advertisement
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) {
- if (l < r) {
- int pivot = partition(a, l, r);
- quicksort(a, l, pivot - 1);
- quicksort(a, pivot + 1, r);
- }
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement