Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- 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);
- }
- }
- void read(int &n, int v[]) {
- cin >> n;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- }
- int main () {
- int n, v[1000];
- read(n, v);
- quicksort(v, 0, n - 1);
- for (int i = 0; i < n; i++)
- cout << v[i] << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement