forked from Gerkins/arithmeticSum
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQuickSort.java
70 lines (60 loc) · 1.66 KB
/
QuickSort.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Created by lrkin on 2016/10/26.
* <p>
* 快速排序
*/
public class QuickSort {
public static void quick_sort(int[] array) {
int length = array.length;
_quick_sort(array, 0, length - 1);
}
public static void _quick_sort(int[] array, int start, int end) {
if(start>=end){
return;
}
int key = array[start];
int i = start;
int j = end;
boolean flag = false;
while (i != j) {
//flag为true,则移动左边(左边和右边比较,key在右边)
if (flag) {
if (key < array[i]) {
swap(array, i, j);
flag = false;
} else {
i++;
}
} else {
//flag为false,则移动右边(右边和左边比较,key在左边)
if (key > array[j]) {
swap(array, i, j);
flag = true;
} else {
j--;
}
}
}
//打印
snp(array);
System.out.println("");
//递归
_quick_sort(array, start, j - 1);
_quick_sort(array, i + 1, end);
}
private static void snp(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+"-");
}
}
private static void swap(int[] arrays, int i, int j) {
int temp;
temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
}
public static void main(String[] args) {
int[] array = {23, 12, 56, 3, 5, 44, 78};
quick_sort(array);
}
}