// This program compares the runtime of various sorting algorithms import java.util.*; // for Random import java.util.function.*; public class Sorting { private static final Random RAND = new Random(); public static void main(String[] args) { // Uses advanced material to swap out which sorting algorithm we're testing System.out.println("Merge sort"); testSort(Sorting::mergeSort); } // Rearranges the elements of a into sorted order using the // merge sort algorithm. public static void mergeSort(String[] a) { if (a.length > 1) { int midpoint = a.length / 2; String[] left = Arrays.copyOfRange(a, 0, midpoint); String[] right = Arrays.copyOfRange(a, midpoint, a.length); mergeSort(left); mergeSort(right); merge(a, left, right); } } // Merges the contents of sorted lists left and right into result, // preserving sorted order. // pre: result.length = left.length + right.length; left is sorted; right is // sorted private static void merge(String[] result, String[] left, String[] right) { int leftIndex = 0; int rightIndex = 0; for (int i = 0; i < result.length; i++) { if (rightIndex >= right.length || leftIndex < left.length && left[leftIndex].compareTo(right[rightIndex]) <= 0) { result[i] = left[leftIndex]; leftIndex++; } else { result[i] = right[rightIndex]; rightIndex++; } } } // Tests different sorting algorithms public static void testSort(Consumer sortFunc) { int length = 10000; // initial length of array to sort int runs = 12; // how many times to double length for (int i = 0; i < runs; i++) { String[] a = createRandomArray(length); // perform a sort and time how long it takes long startTime = System.currentTimeMillis(); // sort here sortFunc.accept(a); long endTime = System.currentTimeMillis(); ensureSorted(a); System.out.printf("%10d elements => %6d ms \n", length, endTime - startTime); length *= 2; // double size of array for next time } } // Rearranges the elements of a into sorted order using // the selection sort algorithm. public static void selectionSort(String[] a) { for (int i = 0; i < a.length - 1; i++) { // look for minimum element int minIndex = i; for (int j = i + 1; j < a.length; j++) { if (a[j].compareTo(a[minIndex]) < 0) { minIndex = j; } } if (minIndex != i) { swap(a, i, minIndex); } } } // pre : 0 <= index1, index2 < list.length // post: Values of list[x] and list[y] are exchanged. private static void swap(String[] list, int index1, int index2) { String temp = list[index1]; list[index1] = list[index2]; list[index2] = temp; } // Rearranges the elements of a into sorted order using // the insertion sort algorithm. public static void insertionSort(String[] a) { for (int i = 1; i < a.length; i++) { // insert element at index i into subarray [0, i-1] String elementToInsert = a[i]; int j = i; // loop backwards and shift values over until the insertion // point is found while (j > 0 && elementToInsert.compareTo(a[j - 1]) < 0) { a[j] = a[j - 1]; j--; } // found the insertion point a[j] = elementToInsert; } } // Returns an array of the given length filled with random values. public static String[] createRandomArray(int length) { String[] a = new String[length]; for (int i = 0; i < a.length; i++) { a[i] = "" + RAND.nextInt(1000000000); } return a; } // Checks whether the given array is in sorted order. // Throws an IllegalStateException if it is not. public static void ensureSorted(String[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i].compareTo(a[i + 1]) > 0) { throw new IllegalStateException("array not sorted at index " + i); } } } }