8
8
* first, middle, and last elements in the list.
9
9
*/
10
10
public class Exercise23_04 {
11
- public static void main ( String [] args ) {
12
- int [] list = { 2 , 3 , 2 , 5 , 6 , 1 , - 2 , 3 , 14 , 12 };
13
- quickSort (list );
14
- System . out . println ( "Sorted List: \n " + Arrays . toString ( list ) );
11
+ public static void quickSort ( int [] list ) {
12
+ /* Exercise 23.4 */
13
+ int medianIdx = indexOfMedian (list );
14
+ quickSort ( list , medianIdx , list . length - 1 );
15
15
}
16
16
17
17
public static void quickSort (int [] list , int first , int last ) {
@@ -22,18 +22,12 @@ public static void quickSort(int[] list, int first, int last) {
22
22
}
23
23
}
24
24
25
- public static void quickSort (int [] list ) {
26
- quickSort (list , 0 , list .length - 1 );
27
- }
28
-
29
25
/**
30
26
* Partition the array list[first..last]
31
27
*/
32
28
public static int partition (int [] list , int first , int last ) {
33
- /* Exercise 23_04 */
34
- int pivotIdx = findMedian (list , first , last );
35
- int pivot = list [pivotIdx ];
36
- int low = first ; // Index for forward search
29
+ int pivot = list [first ]; // Choose the first element as the pivot
30
+ int low = first + 1 ; // Index for forward search
37
31
int high = last ; // Index for backward search
38
32
39
33
while (high > low ) {
@@ -58,33 +52,40 @@ public static int partition(int[] list, int first, int last) {
58
52
59
53
// Swap pivot with list[high]
60
54
if (pivot > list [high ]) {
61
- list [pivotIdx ] = list [high ];
55
+ list [first ] = list [high ];
62
56
list [high ] = pivot ;
63
57
return high ;
64
58
} else {
65
- return pivotIdx ;
59
+ return first ;
66
60
}
67
61
}
68
62
69
63
/**
70
- * Exercise 23.04 solution
71
64
*
72
- * @param list current list
73
- * @param first current left index
74
- * @param last current right index
75
- * @return index of the median between first, last, and mid index values
65
+ * @param list int array to find median of first, middle and last nums
66
+ * @return the index of the median value
76
67
*/
77
- static int findMedian (int [] list , int first , int last ) {
78
- int midIdx = (first + last ) / 2 ;
79
- int firstVal = list [first ];
80
- int lastVal = list [last ];
81
- int midVal = list [midIdx ];
82
- int max = Math .max (Math .max (firstVal , lastVal ), midVal );
83
- int min = Math .min (Math .min (firstVal , lastVal ), midVal );
84
- if (midVal != min && midVal != max ) return midIdx ;
85
- else if (lastVal != min && lastVal != max ) return last ;
86
- else return first ;
68
+ static int indexOfMedian (int [] list ) {
69
+ int [] temp = {list [0 ], list [list .length / 2 ], list [list .length - 1 ]};
70
+ Arrays .sort (temp );
71
+ if (list [0 ] == temp [1 ]) {
72
+ return 0 ;
73
+ }
74
+ else if (list [list .length / 2 ] == temp [1 ]) {
75
+ return list .length / 2 ;
76
+ }
77
+
78
+ return list .length - 1 ;
79
+ }
87
80
81
+ /**
82
+ * A test method
83
+ */
84
+ public static void main (String [] args ) {
85
+ int [] list = {2 , 3 , 2 , 5 , 6 , 1 , -2 , 3 , 14 , 12 };
86
+ quickSort (list );
87
+ for (int i = 0 ; i < list .length ; i ++)
88
+ System .out .print (list [i ] + " " );
88
89
}
89
90
}
90
91
0 commit comments