Skip to content

Commit 1e8a017

Browse files
committed
修改快速排序注释
1 parent 83cd35e commit 1e8a017

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

sort/src/main/java/com/mistray/swapsort/QuickSort.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.mistray.model.SortModel;
44

5+
import java.util.Random;
6+
57
/**
68
* @author MistLight
79
* @create 2018-11-19
@@ -21,7 +23,12 @@
2123
*/
2224
public class QuickSort {
2325
public static void main(String[] args) {
24-
Integer[] arr = {8, 34, 5, 92, 44, 2, 74, 12};
26+
Integer[] arr = new Integer[8];
27+
Random random = new Random();
28+
for (int i = 0; i < 8; i++) {
29+
arr[i] = random.nextInt(150);
30+
}
31+
SortModel.show(arr);
2532
QuickSort.quickSort(arr, 0, arr.length - 1);
2633
SortModel.show(arr);
2734
}
@@ -40,28 +47,35 @@ public static void quickSort(Integer[] arr, Integer start, Integer end) {
4047
}
4148
}
4249

50+
51+
/**
52+
* 根据基准数分治
53+
*
54+
* @param arr
55+
* @param start
56+
* @param end
57+
* @return
58+
*/
4359
private static int partition(Integer[] arr, Integer start, Integer end) {
44-
//arr[start]为挖的第一个坑arr[start]为8
60+
// 将key作为基准数
4561
int key = arr[start];
4662
while (start < end) {
47-
// 从arr[end]开始向前找一个比arr[start]小或相等的数填到arr[end]小的数(arr[5])填到arr[start]的位置
63+
// 找到比key大的
4864
while (arr[end] >= key && end > start) {
4965
end--;
5066
}
51-
// 逆向查找找到[5]
67+
// 把key值放入arr[start]
5268
arr[start] = arr[end];
53-
// {2,34,5,92,44,2,74,12}
5469
SortModel.show(arr);
55-
// 这时又出现了一个新坑(arr[5]),从arr[start]开始向后找一个比arr[5]大的数(arr[1])填到arr[5]的位置
5670
while (arr[start] <= key && end > start) {
5771
start++;
5872
}
59-
// 正向查找找到[1]
73+
// 把arr[start] 放入 arr[end]
6074
arr[end] = arr[start];
6175
SortModel.show(arr);
62-
// 所以会再次进入循环找[1]与[5]未确认的部分,直到start<end为止,将k填进对应的位置
6376
}
6477
arr[start] = key;
78+
SortModel.show(arr);
6579
return start;
6680
}
6781

0 commit comments

Comments
 (0)