Skip to content

Commit d04f4fc

Browse files
update quicksort (#189)
1 parent 3df6dd5 commit d04f4fc

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/main/java/com/examplehub/sorts/QuickSort.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.examplehub.sorts;
22

3-
import com.examplehub.utils.SortUtils;
4-
53
public class QuickSort implements Sort {
64

75
@Override
@@ -11,18 +9,15 @@ public void sort(int[] numbers) {
119

1210
private int partition(int[] number, int left, int right) {
1311
int pivot = number[left];
14-
while (left < right) {
15-
while (left < right && number[right] >= pivot) {
12+
while (left != right) {
13+
while (left != right && number[right] >= pivot) {
1614
right--;
1715
}
18-
while (left < right && number[left] <= pivot) {
19-
left++;
20-
}
21-
if (left < right) {
22-
SortUtils.swap(number, left, right);
16+
number[left] = number[right];
17+
while (left != right && number[left] <= pivot) {
2318
left++;
24-
right--;
2519
}
20+
number[right] = number[left];
2621
}
2722
number[left] = pivot;
2823
return left;
@@ -54,18 +49,15 @@ public <T extends Comparable<T>> void sort(T[] array) {
5449

5550
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
5651
T pivot = array[left];
57-
while (left < right) {
58-
while (left < right && array[right].compareTo(pivot) >= 0) {
52+
while (left != right) {
53+
while (left != right && array[right].compareTo(pivot) >= 0) {
5954
right--;
6055
}
61-
while (left < right && array[left].compareTo(pivot) <= 0) {
56+
array[left] = array[right];
57+
while (left != right && array[left].compareTo(pivot) <= 0) {
6258
left++;
6359
}
64-
if (left < right) {
65-
SortUtils.swap(array, left, right);
66-
left++;
67-
right--;
68-
}
60+
array[right] = array[left];
6961
}
7062
array[left] = pivot;
7163
return left;

0 commit comments

Comments
 (0)