9
9
public class DualPivotQuickSort implements SortAlgorithm {
10
10
11
11
/**
12
- * This method implements the Dual pivot Quick Sort
12
+ * Sorts an array using the Dual Pivot QuickSort algorithm.
13
13
*
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
15
17
*/
16
18
@ 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
+
18
24
dualPivotQuicksort (array , 0 , array .length - 1 );
19
25
return array ;
20
26
}
21
27
22
28
/**
23
- * The sorting process
29
+ * Recursively applies the Dual Pivot QuickSort algorithm to subarrays.
24
30
*
25
- * @param left The first index of an array
26
- * @param right The last index of an array
27
31
* @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
28
35
*/
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 ) {
30
37
if (left < right ) {
31
- int [] pivots = partition (array , left , right );
38
+ final int [] pivots = partition (array , left , right );
32
39
33
40
dualPivotQuicksort (array , left , pivots [0 ] - 1 );
34
41
dualPivotQuicksort (array , pivots [0 ] + 1 , pivots [1 ] - 1 );
@@ -37,70 +44,53 @@ private static <T extends Comparable<T>> void dualPivotQuicksort(T[] array, int
37
44
}
38
45
39
46
/**
40
- * This method finds the partition indices for an array
47
+ * Partitions the array into three parts using two pivots.
41
48
*
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
45
54
*/
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 ])) {
48
57
SortUtils .swap (array , left , right );
49
58
}
50
59
51
- T pivot1 = array [left ];
52
- T pivot2 = array [right ];
60
+ final T pivot1 = array [left ];
61
+ final T pivot2 = array [right ];
53
62
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 ;
57
66
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 --;
68
74
}
75
+ SortUtils .swap (array , low , high );
76
+ high --;
69
77
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 ++;
74
81
}
75
82
}
76
-
77
- less ++;
83
+ low ++;
78
84
}
79
- j --;
80
- great ++;
81
- // Bring the pivots to their appropriate positions
82
- SortUtils .swap (array , left , j );
83
- SortUtils .swap (array , right , great );
84
85
85
- // return the pivots' indices
86
- return new int [] { less , great } ;
87
- }
86
+ // Place the pivots in their correct positions
87
+ pivot1End -- ;
88
+ high ++;
88
89
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 );
102
92
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
+ }
106
96
}
0 commit comments