Skip to content

Commit 56b6d16

Browse files
realDuYuanChaogithub-actions
andauthored
refactor quick sort (#95)
* refactor quicksort * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent c0e23bb commit 56b6d16

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed
Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,75 @@
11
package com.examplehub.sorts;
22

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

75
@Override
86
public void sort(int[] numbers) {
97
quickSort(numbers, 0, numbers.length - 1);
108
}
119

10+
private int partition(int[] number, int left, int right) {
11+
int pivot = number[left];
12+
while (left < right) {
13+
while (left < right && number[right] >= pivot) {
14+
right--;
15+
}
16+
number[left] = number[right];
17+
18+
while (left < right && number[left] <= pivot) {
19+
left++;
20+
}
21+
number[right] = number[left];
22+
}
23+
number[left] = pivot;
24+
return left;
25+
}
26+
1227
/**
1328
* QuickSort algorithm implements.
1429
*
1530
* @param numbers the numbers to be sorted.
1631
*/
1732
public void quickSort(int[] numbers, int left, int right) {
18-
if (left >= right) {
19-
return;
20-
}
21-
int pivot = numbers[right]; /* pick last element as pivot */
22-
/* begin partition */
23-
int i = left; /* left pointer */
24-
int j = right - 1; /* right pointer */
25-
while (i <= j) {
26-
while (numbers[i] < pivot) {
27-
i++; /* move i forward */
28-
}
29-
while (j >= 0 && numbers[j] >= pivot) {
30-
j--; /* move j backward */
31-
}
32-
if (i < j) {
33-
SortUtils.swap(numbers, i, j);
34-
i++;
35-
j--;
36-
}
33+
if (left < right) {
34+
int pivotIndex = partition(numbers, left, right);
35+
quickSort(numbers, left, pivotIndex - 1);
36+
quickSort(numbers, pivotIndex + 1, right);
3737
}
38-
39-
SortUtils.swap(numbers, i, right);
40-
41-
quickSort(numbers, left, i - 1);
42-
quickSort(numbers, i + 1, right);
4338
}
4439

40+
/**
41+
* Generic quickSort algorithm implements.
42+
*
43+
* @param array the array to be sorted.
44+
* @param <T> the class of the objects in the array.
45+
*/
4546
@Override
4647
public <T extends Comparable<T>> void sort(T[] array) {
4748
quickSort(array, 0, array.length - 1);
4849
}
4950

50-
public static <T extends Comparable<T>> void quickSort(T[] array, int left, int right) {
51-
if (left >= right) {
52-
return;
53-
}
54-
T pivot = array[right]; /* pick last element as pivot */
55-
/* begin partition */
56-
int i = left; /* left pointer */
57-
int j = right - 1; /* right pointer */
58-
while (i <= j) {
59-
while (array[i].compareTo(pivot) < 0) {
60-
i++; /* move i forward */
51+
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
52+
T pivot = array[left];
53+
while (left < right) {
54+
while (left < right && array[right].compareTo(pivot) >= 0) {
55+
right--;
6156
}
62-
while (j >= 0 && array[j].compareTo(pivot) >= 0) {
63-
j--; /* move j backward */
64-
}
65-
if (i < j) {
66-
SortUtils.swap(array, i, j);
67-
i++;
68-
j--;
57+
array[left] = array[right];
58+
59+
while (left < right && array[left].compareTo(pivot) <= 0) {
60+
left++;
6961
}
62+
array[right] = array[left];
7063
}
64+
array[left] = pivot;
65+
return left;
66+
}
7167

72-
SortUtils.swap(array, i, right);
73-
74-
quickSort(array, left, i - 1);
75-
quickSort(array, i + 1, right);
68+
public static <T extends Comparable<T>> void quickSort(T[] array, int left, int right) {
69+
if (left < right) {
70+
int pivotIndex = partition(array, left, right);
71+
quickSort(array, left, pivotIndex - 1);
72+
quickSort(array, pivotIndex + 1, right);
73+
}
7674
}
7775
}

0 commit comments

Comments
 (0)