-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsort.cpp
More file actions
57 lines (46 loc) · 971 Bytes
/
qsort.cpp
File metadata and controls
57 lines (46 loc) · 971 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
55
56
57
#include <iostream>
using std::cout;
using std::endl;
// void init(int* a, int len) {
// }
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void myprint(int* a, int left, int right) {
for (int i = left; i <= right; ++i) {
cout <<a[i]<<'\t';
}
cout<<endl;
}
int partition(int* a, int left, int right) {
int index = left;
int pivot = a[left];
swap((a+left), (a+right));
// myprint(a, left, right);
for (int i = left; i < right; ++i) {
if (a[i] > pivot) {
swap((a+(index++)), (a+i));
}
}
swap((a+right), (a+index));
// myprint(a, left, right);
return index;
}
void qsort(int* a, int left, int right) {
if (left >= right || a == NULL) {
return;
}
int index = partition(a, left, right);
qsort(a, left, index-1);
qsort(a, index+1, right);
}
int main(int, char**) {
int a[] = {4, 5, 8, 1, 3, 2};
myprint(a, 0, 5);
// init(a, 6);
qsort(a, 0, 5);
myprint(a, 0, 5);
return 0;
}