-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.h
More file actions
54 lines (29 loc) · 664 Bytes
/
Copy pathquicksort.h
File metadata and controls
54 lines (29 loc) · 664 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
49
50
51
52
53
54
#pragma once
int partition(int* a, int left, int right){
int x =a[left];
left -= 1;
right += 1;
while(true){
while(true){
left++;
if(a[left] >= x) break;
}
while(true){
right--;
if(a[right] <= x) break;
}
if(left < right){
std::swap(a[right],a[left]);
} else {
break;
}
}
return right;
}
void quicksort(int* a, int n, int left, int right){
if(left < right){
int pivot = partition(a,left,right);
quicksort(a,n,left,pivot);
quicksort(a,n,pivot+1,right);
}
}