-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick sort
50 lines (45 loc) · 981 Bytes
/
quick sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int partition(int A[], int lb, int ub) {
int pivot = A[lb];
int start = lb;
int end = ub;
while (start < end) {
while (A[start] <= pivot) {
start++;
}
while (A[end] > pivot) {
end--;
}
if (start < end) {
swap(A[start], A[end]);
}
}
swap(A[lb], A[end]);
return end;
}
void quicksort(int A[], int lb, int ub) {
if (lb < ub) {
int loc = partition(A, lb, ub);
quicksort(A, lb, loc - 1);
quicksort(A, loc + 1, ub);
}
}
int main() {
int n;
cout << "Enter the array size: ";
cin >> n;
int a[n];
cout << "Enter array values: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
quicksort(a, 0, n - 1);
cout << "Sorted array: ";
for (int z = 0; z < n; z++) {
cout << a[z] << " ";
}
cout << endl;
return 0;
}