public class ComparisonSort
extends java.lang.Object
| Constructor and Description | 
|---|
| ComparisonSort() | 
| Modifier and Type | Method and Description | 
|---|---|
| static <E extends java.lang.Comparable<E>>  | heapSort(E[] A)Sorts the given array using the heap sort algorithm outlined below. | 
| static <E extends java.lang.Comparable<E>>  | insertion2Sort(E[] A)Extra Credit: Sorts the given array using the insertion2 sort 
 algorithm outlined below. | 
| static <E extends java.lang.Comparable<E>>  | insertionSort(E[] A)Sorts the given array using the insertion sort algorithm. | 
| static <E extends java.lang.Comparable<E>>  | mergeSort(E[] A)Sorts the given array using the merge sort algorithm. | 
| static <E extends java.lang.Comparable<E>>  | quickSort(E[] A)Sorts the given array using the quick sort algorithm, using the median of
 the first, last, and middle values in each segment of the array as the
 pivot value. | 
| static void | runAllSorts(SortObject[] A)Sorts the given array using the six (seven with the extra credit)
 different sorting algorithms and prints out statistics. | 
| static <E extends java.lang.Comparable<E>>  | selection2Sort(E[] A)Sorts the given array using the selection2 sort algorithm outlined
 below. | 
| static <E extends java.lang.Comparable<E>>  | selectionSort(E[] A)Sorts the given array using the selection sort algorithm. | 
public static <E extends java.lang.Comparable<E>> void selectionSort(E[] A)
E - the type of values to be sortedA - the array to sortpublic static <E extends java.lang.Comparable<E>> void insertionSort(E[] A)
E - the type of values to be sortedA - the array to sortpublic static <E extends java.lang.Comparable<E>> void mergeSort(E[] A)
E - the type of values to be sortedA - the array to sortpublic static <E extends java.lang.Comparable<E>> void quickSort(E[] A)
E - the type of values to be sortedA - the array to sortpublic static <E extends java.lang.Comparable<E>> void heapSort(E[] A)
The heap sort algorithm is:
 for each i from 1 to the end of the array
     insert A[i] into the heap (contained in A[0]...A[i-1])
     
 for each i from the end of the array up to 1
     remove the max element from the heap and put it in A[i]
 E - the type of values to be sortedA - the array to sortpublic static <E extends java.lang.Comparable<E>> void selection2Sort(E[] A)
The selection2 sort is a bi-directional selection sort that sorts the array from the two ends towards the center. The selection2 sort algorithm is:
 begin = 0, end = A.length-1
 
 // At the beginning of every iteration of this loop, we know that the 
 // elements in A are in their final sorted positions from A[0] to A[begin-1]
 // and from A[end+1] to the end of A.  That means that A[begin] to A[end] are
 // still to be sorted.
 do
     use the MinMax algorithm (described below) to find the minimum and maximum 
     values between A[begin] and A[end]
     
     swap the maximum value and A[end]
     swap the minimum value and A[begin]
     
     ++begin, --end
 until the middle of the array is reached
 
 The MinMax algorithm allows you to find the minimum and maximum of N elements in 3N/2 comparisons (instead of 2N comparisons). The way to do this is to keep the current min and max; then
E - the type of values to be sortedA - the array to sortpublic static <E extends java.lang.Comparable<E>> void insertion2Sort(E[] A)
The insertion2 sort is a bi-directional insertion sort that sorts the array from the center out towards the ends. The insertion2 sort algorithm is:
 precondition: A has an even length
 left = element immediately to the left of the center of A
 right = element immediately to the right of the center of A
 if A[left] > A[right]
     swap A[left] and A[right]
 left--, right++ 
  
 // At the beginning of every iteration of this loop, we know that the elements
 // in A from A[left+1] to A[right-1] are in relative sorted order.
 do
     if (A[left] > A[right])
         swap A[left] and A[right]
  
     starting with with A[right] and moving to the left, use insertion sort 
     algorithm to insert the element at A[right] into the correct location 
     between A[left+1] and A[right-1]
     
     starting with A[left] and moving to the right, use the insertion sort 
     algorithm to insert the element at A[left] into the correct location 
     between A[left+1] and A[right-1]
  
     left--, right++
 until left has gone off the left edge of A and right has gone off the right 
       edge of A
 
 This sorting algorithm described above only works on arrays of even length. If the array passed in as a parameter is not even, the method throws an IllegalArgumentException
A - the array to sortjava.lang.IllegalArgumentException - if the length or A is not evenpublic static void runAllSorts(SortObject[] A)
The statistics displayed for each sort are: number of comparisons, number of data moves, and time (in milliseconds).
Note: each sort is given the same array (i.e., in the original order) and the input array A is not changed by this method.
A - the array to sort