-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick.java
37 lines (34 loc) · 883 Bytes
/
Quick.java
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
package com.nifx.book;
import java.util.Arrays;
public class Quick {
public static void sort(int[] a){
sort(a, 0, a.length -1);
}
public static void sort(int[] a, int lo, int hi){
if (hi <= lo) return;
int j = partition(a, lo, hi);
sort(a, lo, j - 1);
sort(a, j + 1, hi);
}
private static int partition(int[] a, int lo, int hi){
int i = lo, j = hi + 1;
int v = a[lo];
while (true){
while (a[++i] < v) if (i == hi) break;
while (v < a[--j]) if (j == lo) break;
if (i >= j) break;
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
private static void exch(int[] a, int i, int j){
int t = a[i]; a[i] = a[j]; a[j] = t;
}
# 测试
public static void main(String[] args) {
int[] testcase = new int[]{10,2,3,6,3,2,5,7,4,10,2,3,6,3,2,5,7,4,31,41,41,2,43,5,4,6,7,5};
Quick.sort(testcase);
System.out.println(Arrays.toString(testcase));
}
}