Skip to content

Commit 63e22d5

Browse files
committed
quickSort optimizergit add -A!
1 parent 34d3825 commit 63e22d5

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/main/java/leetcode/sort/QuikSort.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package leetcode.sort;
22

33
public class QuikSort {
4+
/**
5+
* 原始算法
6+
* @param arr
7+
* @param low
8+
* @param high
9+
* @return
10+
*/
411
int partition(int[] arr, int low, int high) {
512
int base = arr[low];
613

@@ -21,9 +28,34 @@ int partition(int[] arr, int low, int high) {
2128
return low;
2229
}
2330

31+
/**
32+
* 改进算法
33+
* @param arr
34+
* @param low
35+
* @param high
36+
* @return
37+
*/
38+
int partition1(int[] arr, int low, int high) {
39+
int r0 = arr[low];
40+
int base = arr[low];
41+
42+
while (low < high) {
43+
while (low < high && arr[high] >= base) {
44+
high--;
45+
}
46+
arr[low]=arr[high];
47+
while (low < high && arr[low] <= base) {
48+
low++;
49+
}
50+
arr[high]=arr[low];
51+
}
52+
arr[low]=r0;
53+
return low;
54+
}
55+
2456
int[] quickSort(int[] arr, int low, int high) {
2557
if (low <high) {
26-
int base = partition(arr, low, high);
58+
int base = partition1(arr, low, high);
2759
quickSort(arr, 0, base - 1);
2860
quickSort(arr, base + 1, high);
2961
}

0 commit comments

Comments
 (0)