Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
46 lines (37 loc) · 1.04 KB

File metadata and controls

46 lines (37 loc) · 1.04 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package array;
import java.util.Arrays;
/**
* Created by yahier on 17/3/29.
* 快速排序
*/
public class FastSort {
public static void main(String[] args) {
int[] data = {1, 6, 2, 8, 3, 4, 1};
sort(data, 0, data.length - 1);
System.out.println(Arrays.toString(data));
}
public static void sort(int[] array, int lo, int hi) {
if (lo >= hi) {
return;
}
int index = partition(array, lo, hi);
sort(array, lo, index - 1);
sort(array, index + 1, hi);
}
private static int partition(int[] array, int lo, int hi) {
//固定的切分方式
int key = array[lo];
while (lo < hi) {
while (array[hi] >= key && hi > lo) {//从后半部分向前扫描
hi--;
}
array[lo] = array[hi];
while (array[lo] <= key && hi > lo) {//从前半部分向后扫描
lo++;
}
array[hi] = array[lo];
}
array[hi] = key;
return hi;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.