// Hunter Schafer, CSE 143 // This program compares the runtime of the selection and merge sort algorithms. import java.util.*; // for Random /* merge sort 10000 elements => 5 ms 20000 elements => 5 ms 40000 elements => 9 ms 80000 elements => 15 ms 160000 elements => 35 ms 320000 elements => 50 ms 640000 elements => 112 ms 1280000 elements => 234 ms 2560000 elements => 468 ms 5120000 elements => 963 ms selection sort 10000 elements => 38 ms 20000 elements => 130 ms 40000 elements => 532 ms 80000 elements => 2138 ms 160000 elements => 8642 ms 320000 elements => 34630 ms 640000 elements => 374771 ms 1280000 elements => 546499 ms 2560000 elements => ... (too long) 5120000 elements => ... */ public class Sorting { private static final Random RAND = new Random(); public static void main(String[] args) { int length = 10000; // initial length of array to sort int runs = 10; // how many times to double length for (int i = 0; i < runs; i++) { int[] a = createRandomArray(length); // perform a sort and time how long it takes long startTime = System.currentTimeMillis(); // sort here mergeSort(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(int[] 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] < a[minIndex]) { minIndex = j; } } if (minIndex != i) { // swap minimum element to the next available index int temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } } } // Rearranges the elements of a into sorted order using // the insertion sort algorithm. public static void insertionSort(int[] a) { for (int i = 1; i < a.length; i++) { // insert element at index i into subarray [0, i-1] int elementToInsert = a[i]; int j = i; // loop backwards and shift values over until the insertion // point is found while (j > 0 && elementToInsert < a[j - 1]) { a[j] = a[j - 1]; j--; } // found the insertion point a[j] = elementToInsert; } } // Rearranges the elements of a into sorted order using // the bubble sort algorithm. public static void bubbleSort(int[] a) { boolean sorted = false; while (!sorted) { sorted = true; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { // found inversion. swap. sorted = false; int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } } } // Rearranges the elements of a into sorted order using the // merge sort algorithm. public static void mergeSort(int[] a) { // base case: lists of length 0 and 1 are already sorted! if (a.length > 1) { int[] left = Arrays.copyOfRange(a, 0, a.length / 2); int[] right = Arrays.copyOfRange(a, a.length / 2, a.length); // sort right, left 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(int[] result, int[] left, int[] right) { int leftIndex = 0; int rightIndex = 0; for (int i = 0; i < result.length; i++) { if (rightIndex >= right.length || leftIndex < left.length && left[leftIndex] < right[rightIndex]) { result[i] = left[leftIndex]; leftIndex++; } else { result[i] = right[rightIndex]; rightIndex++; } } } // Creates an array of the given length, fills it with random // non-negative integers, and returns it. public static int[] createRandomArray(int length) { int[] a = new int[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(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { throw new IllegalStateException("array not sorted at index " + i); } } } }