-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
48 lines (40 loc) · 770 Bytes
/
QuickSort.cpp
File metadata and controls
48 lines (40 loc) · 770 Bytes
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
#include <iostream>
using namespace std;
int divide(int A[], int low, int high) {
int pivot = A[low];
while(low < high) {
while(low < high && A[high] > pivot) {
high--;
}
if(low < high) {
A[low] = A[high];
low++;
}
while(low < high && A[low] <= pivot) {
low++;
}
if(low < high) {
A[high] = A[low];
high--;
}
}
A[low] = pivot;
return low;
}
void quickSort(int A[], int low, int high) {
if(low >= high) return;
int mid = divide(A, low, high);
quickSort(A, low, mid-1);
quickSort(A, mid+1, high);
}
void quickSort(int A[], int size) {
quickSort(A, 0, size-1);
}
int main() {
int A[] = {4, 6, 10, 2, 1, 4, 2, 8};
int n = sizeof(A)/sizeof(int);
quickSort(A, n);
for(int i = 0; i < n; ++i) {
printf("%d ", A[i]);
}
}