Skip to content

Commit 7524d10

Browse files
committed
快排注释完善
1 parent e1beffa commit 7524d10

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,27 @@ public static void quickSort(Integer[] arr, Integer start, Integer end) {
5858
private static int partition(Integer[] arr, Integer start, Integer end) {
5959
// 将key作为基准数
6060
int key = arr[start];
61+
// 从尾向头找,大于key的元素不动,将小于key的值与key替换位置
62+
// 再从头向尾找,小于key的元素不动,将大于key的值与上面找到的arr[end]替换位置
63+
// 直到索引号相同,说明数组中元素全部被检索一遍,key左边的都是比key小的,key右边都是比key大的.
6164
while (start < end) {
62-
// 找到比key大的
65+
SortModel.show(arr);
66+
// 从尾向头找,找到比key小的就结束
6367
while (arr[end] >= key && end > start) {
6468
end--;
6569
}
66-
// 把key值放入arr[start]
70+
// 把上面找到的比key小的值放入arr[start]
6771
arr[start] = arr[end];
68-
SortModel.show(arr);
72+
// 从头向尾找,找到比key大的就结束
6973
while (arr[start] <= key && end > start) {
7074
start++;
7175
}
72-
// 把arr[start] 放入 arr[end]
76+
// 把上面找到的比key大的值放入 arr[end]
7377
arr[end] = arr[start];
7478
SortModel.show(arr);
7579
}
76-
arr[start] = key;
80+
// 最终end=start,说明了整个数组已经被扫描了一遍
81+
arr[end] = key;
7782
SortModel.show(arr);
7883
return start;
7984
}

0 commit comments

Comments
 (0)