Skip to content

Commit 8d0dd3e

Browse files
authored
refactor: cleanup DualPivotQuickSort (#5319)
1 parent 41f76e0 commit 8d0dd3e

File tree

2 files changed

+53
-117
lines changed

2 files changed

+53
-117
lines changed

src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java

+49-59
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,33 @@
99
public class DualPivotQuickSort implements SortAlgorithm {
1010

1111
/**
12-
* This method implements the Dual pivot Quick Sort
12+
* Sorts an array using the Dual Pivot QuickSort algorithm.
1313
*
14-
* @param array The array to be sorted Sorts the array in increasing order
14+
* @param array The array to be sorted
15+
* @param <T> The type of elements in the array, which must be comparable
16+
* @return The sorted array
1517
*/
1618
@Override
17-
public <T extends Comparable<T>> T[] sort(T[] array) {
19+
public <T extends Comparable<T>> T[] sort(final T[] array) {
20+
if (array.length <= 1) {
21+
return array;
22+
}
23+
1824
dualPivotQuicksort(array, 0, array.length - 1);
1925
return array;
2026
}
2127

2228
/**
23-
* The sorting process
29+
* Recursively applies the Dual Pivot QuickSort algorithm to subarrays.
2430
*
25-
* @param left The first index of an array
26-
* @param right The last index of an array
2731
* @param array The array to be sorted
32+
* @param left The starting index of the subarray
33+
* @param right The ending index of the subarray
34+
* @param <T> The type of elements in the array, which must be comparable
2835
*/
29-
private static <T extends Comparable<T>> void dualPivotQuicksort(T[] array, int left, int right) {
36+
private static <T extends Comparable<T>> void dualPivotQuicksort(final T[] array, final int left, final int right) {
3037
if (left < right) {
31-
int[] pivots = partition(array, left, right);
38+
final int[] pivots = partition(array, left, right);
3239

3340
dualPivotQuicksort(array, left, pivots[0] - 1);
3441
dualPivotQuicksort(array, pivots[0] + 1, pivots[1] - 1);
@@ -37,70 +44,53 @@ private static <T extends Comparable<T>> void dualPivotQuicksort(T[] array, int
3744
}
3845

3946
/**
40-
* This method finds the partition indices for an array
47+
* Partitions the array into three parts using two pivots.
4148
*
42-
* @param array The array to be sorted
43-
* @param left The first index of an array
44-
* @param right The last index of an array Finds the partition index of an array
49+
* @param array The array to be partitioned
50+
* @param left The starting index for partitioning
51+
* @param right The ending index for partitioning
52+
* @param <T> The type of elements in the array, which must be comparable
53+
* @return An array containing the indices of the two pivots
4554
*/
46-
private static <T extends Comparable<T>> int[] partition(T[] array, int left, int right) {
47-
if (array[left].compareTo(array[right]) > 0) {
55+
private static <T extends Comparable<T>> int[] partition(final T[] array, int left, final int right) {
56+
if (SortUtils.greater(array[left], array[right])) {
4857
SortUtils.swap(array, left, right);
4958
}
5059

51-
T pivot1 = array[left];
52-
T pivot2 = array[right];
60+
final T pivot1 = array[left];
61+
final T pivot2 = array[right];
5362

54-
int j = left + 1;
55-
int less = left + 1;
56-
int great = right - 1;
63+
int pivot1End = left + 1;
64+
int low = left + 1;
65+
int high = right - 1;
5766

58-
while (less <= great) {
59-
// If element is less than pivot1
60-
if (array[less].compareTo(pivot1) < 0) {
61-
SortUtils.swap(array, less, left++);
62-
}
63-
64-
// If element is greater or equal to pivot2
65-
else if (array[less].compareTo(pivot2) >= 0) {
66-
while (less < great && array[great].compareTo(pivot2) > 0) {
67-
great--;
67+
while (low <= high) {
68+
if (SortUtils.less(array[low], pivot1)) {
69+
SortUtils.swap(array, low, pivot1End);
70+
pivot1End++;
71+
} else if (SortUtils.greaterOrEqual(array[low], pivot2)) {
72+
while (low < high && SortUtils.greater(array[high], pivot2)) {
73+
high--;
6874
}
75+
SortUtils.swap(array, low, high);
76+
high--;
6977

70-
SortUtils.swap(array, less, great--);
71-
72-
if (array[less].compareTo(pivot1) < 0) {
73-
SortUtils.swap(array, less, left++);
78+
if (SortUtils.less(array[low], pivot1)) {
79+
SortUtils.swap(array, low, pivot1End);
80+
pivot1End++;
7481
}
7582
}
76-
77-
less++;
83+
low++;
7884
}
79-
j--;
80-
great++;
81-
// Bring the pivots to their appropriate positions
82-
SortUtils.swap(array, left, j);
83-
SortUtils.swap(array, right, great);
8485

85-
// return the pivots' indices
86-
return new int[] {less, great};
87-
}
86+
// Place the pivots in their correct positions
87+
pivot1End--;
88+
high++;
8889

89-
/**
90-
* Main method
91-
*
92-
* @param args the command line arguments
93-
*/
94-
public static void main(String[] args) {
95-
Integer[] array = {24, 8, -42, 75, -29, -77, 38, 57};
96-
DualPivotQuickSort dualPivotQuickSort = new DualPivotQuickSort();
97-
dualPivotQuickSort.sort(array);
98-
for (int i = 0; i < array.length; i++) {
99-
System.out.print(array[i] + " ");
100-
}
101-
}
90+
SortUtils.swap(array, left, pivot1End);
91+
SortUtils.swap(array, right, high);
10292

103-
/*
104-
* References: https://www.geeksforgeeks.org/dual-pivot-quicksort/
105-
*/
93+
// Return the indices of the pivots
94+
return new int[] {low, high};
95+
}
10696
}
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,8 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
5-
import org.junit.jupiter.api.Test;
6-
7-
/**
8-
* @author Debasish Biswas (https://github.com/debasishbsws)
9-
* @see DualPivotQuickSort
10-
*/
11-
class DualPivotQuickSortTest {
12-
13-
private DualPivotQuickSort dualPivotquickSort = new DualPivotQuickSort();
14-
15-
@Test
16-
void quickSortEmptyArrayShouldPass() {
17-
Integer[] array = {};
18-
Integer[] sorted = dualPivotquickSort.sort(array);
19-
Integer[] expected = {};
20-
assertArrayEquals(expected, sorted);
21-
}
22-
23-
@Test
24-
void quickSortSingleValueArrayShouldPass() {
25-
Integer[] array = {7};
26-
Integer[] sorted = dualPivotquickSort.sort(array);
27-
Integer[] expected = {7};
28-
assertArrayEquals(expected, sorted);
29-
}
30-
31-
@Test
32-
void quickSortWithIntegerArrayShouldPass() {
33-
Integer[] array = {49, 4, 36, 9, 144, 1};
34-
Integer[] sorted = dualPivotquickSort.sort(array);
35-
Integer[] expected = {1, 4, 9, 36, 49, 144};
36-
assertArrayEquals(expected, sorted);
37-
}
38-
39-
@Test
40-
void quickSortForArrayWithNegativeValuesShouldPass() {
41-
Integer[] array = {49, -36, -124, -49, 12, 9};
42-
Integer[] sorted = dualPivotquickSort.sort(array);
43-
Integer[] expected = {-124, -49, -36, 9, 12, 49};
44-
assertArrayEquals(expected, sorted);
45-
}
46-
47-
@Test
48-
void quickSortForArrayWithDuplicateValuesShouldPass() {
49-
Integer[] array = {36, 1, 49, 1, 4, 9};
50-
Integer[] sorted = dualPivotquickSort.sort(array);
51-
Integer[] expected = {1, 1, 4, 9, 36, 49};
52-
assertArrayEquals(expected, sorted);
53-
}
54-
55-
@Test
56-
void quickSortWithStringArrayShouldPass() {
57-
String[] array = {"cat", "ant", "eat", "boss", "dog", "apple"};
58-
String[] sorted = dualPivotquickSort.sort(array);
59-
String[] expected = {"ant", "apple", "boss", "cat", "dog", "eat"};
60-
assertArrayEquals(expected, sorted);
3+
class DualPivotQuickSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new DualPivotQuickSort();
617
}
628
}

0 commit comments

Comments
 (0)