-
Notifications
You must be signed in to change notification settings - Fork 2
/
quick_sort.cpp
52 lines (40 loc) · 936 Bytes
/
quick_sort.cpp
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
50
51
52
#include<iostream>
using namespace std;
int partition(int a[], int low, int high, int pivot) {
int i, j;
i = j = low;
while(i <= high) {
if(a[i] > pivot) i++;
else {
swap(a[i], a[j]);
i++;
j++;
}
}
return j-1;
}
void quickSort(int a[], int low, int high) {
if(low < high) {
int pivot = a[high];
int pos = partition(a, low, high, pivot);
quickSort(a, low, pos-1);
quickSort(a, pos+1, high);
}
}
void print(int a[], int size) {
for(int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
int main() {
int a[] = {5,6,3,2,1,19,16,17,23,18};
int size = sizeof(a) / sizeof(int);
cout << "Before sort : ";
print(a, size);
quickSort(a, 0, size-1);
cout << "After sort : ";
print(a, size);
// quickSort(a, 0, size-1);
return 0;
}