1+ /*********************************************************************************
2+ * (Generic merge sort) Write the following two generic methods using merge 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 mergeSort(E[] list) *
8+ * public static <E> void mergeSort(E[] list, *
9+ * Comparator<? super E> comparator) *
10+ *********************************************************************************/
11+ import java .util .Comparator ;
12+ import java .util .Arrays ;
13+
14+ public class Exercise_23_02 {
15+ /** Generic merge sort using Comparable */
16+ public static <E extends Comparable <E >> void mergeSort (E [] list ) {
17+ if (list .length > 1 ) {
18+ // Merge sort the first half
19+ E [] firstHalf = (E [])new Comparable [list .length / 2 ];
20+ System .arraycopy (list , 0 , firstHalf , 0 , list .length / 2 );
21+ mergeSort (firstHalf );
22+
23+ // Merge sort the second half
24+ int secondHalfLength = list .length - list .length / 2 ;
25+ E [] secondHalf = (E [])(new Comparable [secondHalfLength ]);
26+ System .arraycopy (list , list .length / 2 ,
27+ secondHalf , 0 , secondHalfLength );
28+ mergeSort (secondHalf );
29+
30+ // Merge firstHalf with secondHalf into list
31+ merge (firstHalf , secondHalf , list );
32+ }
33+ }
34+
35+ /** Merge two sorted lists */
36+ public static <E extends Comparable <E >> void merge (E [] list1 , E [] list2 , E [] temp ) {
37+ int current1 = 0 ; // Current index in list1
38+ int current2 = 0 ; // Current index in list2
39+ int current3 = 0 ; // Current index in temp
40+
41+ while (current1 < list1 .length && current2 < list2 .length ) {
42+ if (list1 [current1 ].compareTo (list2 [current2 ]) < 0 )
43+ temp [current3 ++] = list1 [current1 ++];
44+ else
45+ temp [current3 ++] = list2 [current2 ++];
46+ }
47+
48+ while (current1 < list1 .length )
49+ temp [current3 ++] = list1 [current1 ++];
50+
51+ while (current2 < list2 .length )
52+ temp [current3 ++] = list2 [current2 ++];
53+ }
54+
55+ public static <E > void mergeSort (E [] list , Comparator <? super E > comparator ) {
56+ /** Generic mergeSort using Comparator */
57+ if (list .length > 1 ) {
58+ // Merge sort the first half
59+ E [] firstHalf = Arrays .copyOf (list , list .length / 2 );
60+ mergeSort (firstHalf , comparator );
61+
62+ // Merge sort the second half
63+ E [] secondHalf = Arrays .copyOfRange (list , list .length / 2 , list .length );
64+ mergeSort (secondHalf , comparator );
65+
66+ // Merge firstHalf with secondHalf into list
67+ merge (firstHalf , secondHalf , list , comparator );
68+ }
69+ }
70+
71+ /** Merge two sorted lists */
72+ public static <E > void merge (E [] list1 , E [] list2 , E [] temp ,
73+ Comparator <? super E > comparator ) {
74+ int current1 = 0 ; // Current index in list1
75+ int current2 = 0 ; // Current index in list2
76+ int current3 = 0 ; // Current index in temp
77+
78+ while (current1 < list1 .length && current2 < list2 .length ) {
79+ if (comparator .compare (list1 [current1 ], list2 [current2 ]) < 0 )
80+ temp [current3 ++] = list1 [current1 ++];
81+ else
82+ temp [current3 ++] = list2 [current2 ++];
83+ }
84+
85+ while (current1 < list1 .length )
86+ temp [current3 ++] = list1 [current1 ++];
87+
88+ while (current2 < list2 .length )
89+ temp [current3 ++] = list2 [current2 ++];
90+ }
91+
92+ /** A test method */
93+ public static void main (String [] args ) {
94+ Integer [] intArray = {2 , 3 , 2 , 5 , 6 , 1 , -2 , 3 , 14 , 12 };
95+
96+ // Create a Double array
97+ Double [] doubleArray = {3.4 , 1.3 , -22.1 , 14.8 , 6.0 , 2.3 , 12.2 };
98+
99+ // Create a Character array
100+ Character [] charArray = {'a' , 'J' , 'r' };
101+
102+ // Create a String array
103+ String [] stringArray = {"Tom" , "Susan" , "Kim" };
104+
105+ // Sort the arrays
106+ mergeSort (intArray );
107+ mergeSort (doubleArray );
108+ mergeSort (charArray );
109+ mergeSort (stringArray );
110+
111+ // Display the arrays
112+ printList (intArray );
113+ printList (charArray );
114+ printList (stringArray );
115+ printList (doubleArray );
116+
117+ // Create an array of 10 GeometricObjects
118+ GeometricObject [] list = {new Circle (5 ), new Rectangle (4 , 5 ),
119+ new Circle (5.5 ), new Rectangle (2.4 , 5 ), new Circle (0.5 ),
120+ new Rectangle (4 , 65 ), new Circle (4.5 ), new Rectangle (4.4 , 1 ),
121+ new Circle (6.5 ), new Rectangle (4 , 5 )};
122+
123+ // Invoke merge sort using GeometricObjectComparator
124+ mergeSort (list , new GeometricObjectComparator ());
125+
126+ // Display the sorted elements
127+ printList (list );
128+ }
129+
130+ /** Print an array of Objects */
131+ public static void printList (Object [] list ) {
132+ for (int i = 0 ; i < list .length ; i ++)
133+ System .out .print (list [i ] + " " );
134+ System .out .println ();
135+ }
136+
137+ /** Print the sorted elements */
138+ public static void printList (GeometricObject [] list ) {
139+ System .out .print ("Sorted elements: " );
140+ for (GeometricObject e : list ) {
141+ System .out .printf ("%.2f " , e .getArea ());
142+ }
143+ System .out .println ();
144+ }
145+ }
0 commit comments