Java Reference
In-Depth Information
P ROGRAMMING E XERCISES
Sections 23.3-23.5
23.1
( Generic bubble sort ) Write the following two generic methods using bubble
sort. The first method sorts the elements using the Comparable interface and
the second uses the Comparator interface.
public static <E extends Comparable<E>>
void bubbleSort(E[] list)
public static <E> void bubbleSort(E[] list,
Comparator<? super E> comparator)
23.2
( Generic merge sort ) Write the following two generic methods using merge sort.
The first method sorts the elements using the Comparable interface and the
second uses the Comparator interface.
public static <E extends Comparable<E>>
void mergeSort(E[] list)
public static <E> void mergeSort(E[] list,
Comparator<? super E> comparator)
23.3
( Generic quick sort ) Write the following two generic methods using quick sort.
The first method sorts the elements using the Comparable interface and the
second uses the Comparator interface.
public static <E extends Comparable<E>>
void quickSort(E[] list)
public static <E> void quickSort(E[] list,
Comparator<? super E> comparator)
23.4
( Improve quick sort ) The quick sort algorithm presented in the topic selects the
first element in the list as the pivot. Revise it by selecting the median among the
first, middle, and last elements in the list.
*23.5
( Generic heap sort ) Write the following two generic methods using heap sort.
The first method sorts the elements using the Comparable interface and the
second uses the Comparator interface.
public static <E extends Comparable<E>>
void heapSort(E[] list)
public static <E> void heapSort(E[] list,
Comparator<? super E> comparator)
23.6
( Check order ) Write the following overloaded methods that check whether an
array is ordered in ascending order or descending order. By default, the method
checks ascending order. To check descending order, pass false to the ascend-
ing argument in the method.
public static boolean ordered( int [] list)
public static boolean ordered( int [] list, boolean ascending)
public static boolean ordered( double [] list)
public static boolean ordered
( double [] list, boolean ascending)
public static <E extends Comparable<E>>
boolean ordered(E[] list)
public static <E extends Comparable<E>> boolean ordered
(E[] list, boolean ascending)
public static <E> boolean ordered(E[] list,
 
 
Search WWH ::




Custom Search