1
+ /*********************************************************************************
2
+ * (Generic quick sort) Write the following two generic methods using quick sort. *
3
+ * The first method sorts the elements using the Comparable interface and the *
4
+ * second uses the Comparator interface. *
5
+ * *
6
+ * public static <E extends Comparable<E>> *
7
+ * void quickSort(E[] list) *
8
+ * public static <E> void quickSort(E[] list, *
9
+ * Comparator<? super E> comparator) *
10
+ *********************************************************************************/
11
+ import java .util .Comparator ;
12
+
13
+ public class Exercise_23_03 {
14
+ /** Generic quick sort using Comparable */
15
+ public static <E extends Comparable <E >> void quickSort (E [] list ) {
16
+ quickSort (list , 0 , list .length - 1 );
17
+ }
18
+
19
+ public static <E extends Comparable <E >>
20
+ void quickSort (E [] list , int first , int last ) {
21
+ if (last > first ) {
22
+ int pivotIndex = partition (list , first , last );
23
+ quickSort (list , first , pivotIndex - 1 );
24
+ quickSort (list , pivotIndex + 1 , last );
25
+ }
26
+ }
27
+
28
+ /** Partition the array list[first..last] */
29
+ public static <E extends Comparable <E >>
30
+ int partition (E [] list , int first , int last ) {
31
+ E pivot = list [first ]; // Choose the first element as the pivot
32
+ int low = first + 1 ; // Index for forward search
33
+ int high = last ; // Index for backward search
34
+
35
+ while (high > low ) {
36
+ // Search forward from left
37
+ while (low <= high && list [low ].compareTo (pivot ) <= 0 )
38
+ low ++;
39
+
40
+ // Search backward from right
41
+ while (low <= high && list [high ].compareTo (pivot ) > 0 )
42
+ high --;
43
+
44
+ // Swap two elements in the list
45
+ if (high > low ) {
46
+ E temp = list [high ];
47
+ list [high ] = list [low ];
48
+ list [low ] = temp ;
49
+ }
50
+ }
51
+
52
+ while (high > first && list [high ].compareTo (pivot ) >= 0 )
53
+ high --;
54
+
55
+ // Swap pivot with list[high]
56
+ if (pivot .compareTo (list [high ]) > 0 ) {
57
+ list [first ] = list [high ];
58
+ list [high ] = pivot ;
59
+ return high ;
60
+ }
61
+ else {
62
+ return first ;
63
+ }
64
+ }
65
+
66
+ public static <E > void quickSort (E [] list , Comparator <? super E > comparator ) {
67
+ quickSort (list , 0 , list .length - 1 , comparator );
68
+ }
69
+
70
+ public static <E > void quickSort (
71
+ E [] list , int first , int last , Comparator <? super E > comparator ) {
72
+ if (last > first ) {
73
+ int pivotIndex = partition (list , first , last , comparator );
74
+ quickSort (list , first , pivotIndex - 1 , comparator );
75
+ quickSort (list , pivotIndex + 1 , last , comparator );
76
+ }
77
+ }
78
+
79
+ /** Partition the array list[first.. last] */
80
+ public static <E > int partition (
81
+ E [] list , int first , int last , Comparator <? super E > comparator ) {
82
+ E pivot = list [first ]; // Choose the first element as the pivot
83
+ int low = first + 1 ; // Index for forward search
84
+ int high = last ; // Index for backward search
85
+
86
+ while (high > low ) {
87
+ // Search forward from left
88
+ while (low <= high && comparator .compare (list [low ], pivot ) <= 0 )
89
+ low ++;
90
+
91
+ // Search backward from right
92
+ while (low <= high && comparator .compare (list [high ], pivot ) > 0 )
93
+ high --;
94
+
95
+ // Swap two elements in the list
96
+ if (high > low ) {
97
+ E temp = list [high ];
98
+ list [high ] = list [low ];
99
+ list [low ] = temp ;
100
+ }
101
+ }
102
+
103
+ while (high > first && comparator .compare (list [high ], pivot ) >= 0 )
104
+ high --;
105
+
106
+ // Swap pivot with list[high]
107
+ if (comparator .compare (pivot , list [high ]) > 0 ) {
108
+ list [first ] = list [high ];
109
+ list [high ] = pivot ;
110
+ return high ;
111
+ }
112
+ else {
113
+ return first ;
114
+ }
115
+ }
116
+
117
+ /** A test method */
118
+ public static void main (String [] args ) {
119
+ // Create an Integer array
120
+ Integer [] intArray = {2 , 3 , 2 , 5 , 6 , 1 , -2 , 3 , 14 , 12 };
121
+
122
+ // Create a Double array
123
+ Double [] doubleArray = {3.4 , 1.3 , -22.1 , 14.8 , 6.0 , 2.3 , 12.2 };
124
+
125
+ // Create a Character array
126
+ Character [] charArray = {'a' , 'J' , 'r' };
127
+
128
+ // Create a String array
129
+ String [] stringArray = {"Tom" , "Susan" , "Kim" };
130
+
131
+ // Sort the arrays
132
+ quickSort (intArray );
133
+ quickSort (doubleArray );
134
+ quickSort (charArray );
135
+ quickSort (stringArray );
136
+
137
+ // Display the sorted arrays
138
+ printList (intArray );
139
+ printList (doubleArray );
140
+ printList (charArray );
141
+ printList (stringArray );
142
+
143
+ // Create an array of 10 GeometricObjects
144
+ GeometricObject [] list = {new Circle (5 ), new Rectangle (4 , 5 ),
145
+ new Circle (5.5 ), new Rectangle (2.4 , 5 ), new Circle (0.5 ),
146
+ new Rectangle (4 , 65 ), new Circle (4.5 ), new Rectangle (4.4 , 1 ),
147
+ new Circle (6.5 ), new Rectangle (4 , 5 )};
148
+
149
+ // Invoke quick sort using GeometricObjectComparator
150
+ quickSort (list , new GeometricObjectComparator ());
151
+
152
+ // Display the sorted elements
153
+ printList (list );
154
+ }
155
+
156
+ /** Print an array elements */
157
+ public static void printList (Object [] list ) {
158
+ for (int i = 0 ; i < list .length ; i ++) {
159
+ System .out .print (list [i ] + " " );
160
+ }
161
+ System .out .println ();
162
+ }
163
+
164
+ /** Print an array of elements */
165
+ public static void printList (GeometricObject [] list ) {
166
+ System .out .print ("Sorted elements: " );
167
+ for (GeometricObject e : list ) {
168
+ System .out .printf ("%.2f " , e .getArea ());
169
+ }
170
+ System .out .println ();
171
+ }
172
+ }
0 commit comments