-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQSort.cc
More file actions
48 lines (45 loc) · 919 Bytes
/
Copy pathQSort.cc
File metadata and controls
48 lines (45 loc) · 919 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>
#include <vector>
using namespace std;
class Solution {
public:
void qsort(int *A, int n) {
int m = 0, i = 0, j = n-1, t;
if (n <= 1) return;
if (n == 2) {
if (A[0] > A[1]){
t = A[0];
A[0] = A[1];
A[1] = t;
}
return;
}
while (i != j) {
if (j != m) {
if (A[j] < A[m]) {
t = A[j];
A[j] = A[m];
A[m] = t;
m = j;
} else --j;
} else {
if (A[i] >= A[m]) {
t = A[i];
A[i] = A[m];
A[m] = t;
m = i;
} else ++i;
}
}
qsort(A, i);
qsort(A+i+1, n-i-1);
}
};
int main(int argc, char **argv) {
Solution s;
int A[] = {2, 3, 4, 2, 3, 5, 5, 4, 1, 9 ,11, 9};
s.qsort(A, 12);
for (int i=0; i < 12; ++i) {
cout << A[i] << " ";
}
}