Skip to content

Commit b2bfa48

Browse files
Merge pull request wangzheng0822#275 from luanheart/master
fix bug java.12_sorts.KthSmallest
2 parents bafb6f5 + db78ce3 commit b2bfa48

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

java/12_sorts/KthSmallest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ private static int partition(int[] arr, int p, int r) {
2727
int pivot = arr[r];
2828

2929
int i = p;
30-
for (int j = p; j <= r - 1; j++) {
31-
if (arr[j] < pivot) {
30+
for (int j = p; j < r; j++) {
31+
// 这里要是 <= ,不然会出现死循环,比如查找数组 [1,1,2] 的第二小的元素
32+
if (arr[j] <= pivot) {
3233
swap(arr, i, j);
3334
i++;
3435
}

0 commit comments

Comments
 (0)