Skip to content

Commit

Permalink
Update QuickSort.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezreal147 authored May 24, 2022
1 parent 88ae278 commit a1eaf2f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/learning/sort/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void sort1(int[] arr, int start, int end) {
* @param right
*/
public static void sort(int[] arr, int left, int right) {
if (left > right) return;
if (left >= right) return;
int pos = partition(arr, left, right);
sort(arr, left, pos - 1);
sort(arr, pos + 1, right);
Expand All @@ -72,7 +72,13 @@ public static int partition(int[] arr, int left, int right) {
if(i>=j) break;
swap(arr, i, j);
}
swap(arr, i, left);
//先判断要交换的节点是否比基节点大
if (arr[left] > arr[i]) {
swap(arr, left, i);
} else if (i - 1 >= 0) {
swap(arr, left, i - 1);
i--;
}
return i;
}

Expand Down

0 comments on commit a1eaf2f

Please sign in to comment.