forked from nibnait/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe_Quick.java
More file actions
52 lines (44 loc) · 1.16 KB
/
e_Quick.java
File metadata and controls
52 lines (44 loc) · 1.16 KB
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
package Algorithms_4thEdition.a_Sorting;
import Standard.std;
import Standard.stdOut;
/**
* Created by nibnait on 2016/8/9.
*/
public class e_Quick {
public static void main(String[] args) {
// int[] a = new int[10];
// a = stdRandom.random(a);
int[] a = {3,4,0,1,6,2,5,1};
stdOut.print(a);
a = Quick_Sort(a, 0, a.length-1);
stdOut.print(a);
}
/**
*
*/
public static int[] Quick_Sort(int[] a, int lo, int hi) {
if (lo>=hi)
return a;
int j = partition(a, lo, hi);
Quick_Sort(a, lo, j-1);
Quick_Sort(a, j+1, hi);
return a;
}
private static int partition(int[] a, int lo, int hi) {
// int flag = lo;
// for (int i = lo; i <= hi; i++) {
// if (a[i] < a[hi]){ //以a[hi]为基准
// std.swap(a, i, flag++);
// }
// }
// std.swap(a,flag,hi);
int flag = lo+1; //以a[lo]为基准
for (int i = lo+1; i <= hi; i++) {
if (a[i]<a[lo]) {
std.swap(a,i,flag++);
}
}
std.swap(a,--flag,lo);
return flag;
}
}