-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
47 lines (42 loc) · 1.29 KB
/
QuickSort.java
File metadata and controls
47 lines (42 loc) · 1.29 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
package sort;
//快速排序
/**
* 快速排序的基本思想是,通过一轮的排序将序列分割成独立的两部分,
* 其中一部分序列的关键字(这里主要用值来表示)均比另一部分关键字小
*/
public class QuickSort {
public void sort(Comparable[] a, int lo, int hi) {
if (lo >= hi) return;
int j = partition(a, lo, hi);
sort(a, lo, j-1);
sort(a, j+1, hi);
}
private int partition(Comparable[] a, int lo, int hi) {
int i = lo, j = hi+1;
//int mid = lo + (lo + hi)/2;
Comparable re = a[lo];
//以下代码错误:
/*while (i < j) {
if (a[i].compareTo(re) < 0) {
i++;
} else if (a[j].compareTo(re) > 0) {
j--;
} else {
exch(a, i, j);
}
}*/
while (true) {
while (a[++i].compareTo(re) < 0 && i < hi) ;//&&后面代码不能省略
while (a[--j].compareTo(re) > 0 && j > lo) ;
if (i >= j) break;
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
private void exch(Comparable[] a, int i, int j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}