-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGp2_2.cpp
46 lines (40 loc) · 836 Bytes
/
Gp2_2.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
/*
* A implemention for quick sort
* write for group meeting homework h2p2
* 2018年 11月 05日 星期一 22:59:16 CST
*/
#include <iostream>
#include <vector>
using namespace std;
void swap(int& a, int& b){
int tmp = a;
a = b;
b = tmp;
}
int partition(vector<int>& A, int p, int r){
int pivot = A[p-1];
int i = p;
for (int j=p; j!=r; ++j){
if (A[j] < pivot){
// exchange A[storeIdx] with A[i]
swap(A[i], A[j]);
++i;
}
}
// exchange the index of pivot with A[i - 1]
swap(A[p-1], A[i-1]);
return i;
}
void quick_sort(vector<int>& A, int p, int r){
if (p < r){
int q = partition(A, p, r);
quick_sort(A, p, q-1);
quick_sort(A, q+1, r);
}
}
int main(){
vector<int> seq = {2,4,6,9,0,5,7,3,5,7,1,2,3,9,5,8,4,7,6,2};
quick_sort(seq, 1, seq.size());
for (auto i:seq) cout << i << '\n';
return 0;
}