Skip to content

Commit 31194f4

Browse files
committed
Update QuickSort.java
1 parent 6642f5a commit 31194f4

File tree

1 file changed

+0
-52
lines changed

1 file changed

+0
-52
lines changed

src/sort/QuickSort.java

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,6 @@ public static void main(String[] args) {
1717
}
1818

1919

20-
/**
21-
* 传统方法
22-
* 选择left为基准点
23-
*
24-
* @param array
25-
* @param left
26-
* @param right
27-
*/
28-
public void quickSortLeftBase(int[] array, int left, int right) {
29-
// 元素个数小于等于1,无需排序,结束
30-
if (left >= right) {
31-
return;
32-
}
33-
34-
int index = getPartition(array, left, right);
35-
quickSortLeftBase(array, left, index - 1);
36-
quickSortLeftBase(array, index + 1, right);
37-
}
38-
39-
40-
/**
41-
* 传统方法
42-
* 选择left为基准点,设为base
43-
* 先从右边元素开始,找到第一个比base小的,将array[i]设为该值(array[j])。
44-
* 再从左边开始,找到第一个比base大的,将array[j]设为该值(array[i])。
45-
* 结束循环后,将array[i]的值设为base。
46-
*
47-
* @param array
48-
* @param left
49-
* @param right
50-
*/
51-
private int getPartition(int[] array, int left, int right) {
52-
int i = left, j = right;
53-
int baseVal = array[left];
54-
55-
while (i < j) {
56-
while (i < j && array[j] >= baseVal) {
57-
j--;
58-
}
59-
array[i] = array[j];
60-
while (i < j && array[i] <= baseVal) {
61-
i++;
62-
}
63-
array[j] = array[i];
64-
}
65-
66-
array[i] = baseVal;
67-
// i 为基准点坐标
68-
return i;
69-
}
70-
71-
7220
public void sort(int[] nums) {
7321
// 为了避免出现耗时的极端情况,先随机打乱
7422
shuffle(nums);

0 commit comments

Comments
 (0)