// This program has a method called sort that sorts an array of int // values using the insertion sort algorithm. Method main tests it by // constructing two arrays containing random values. It compares the // time used by this sort versus Arrays.sort. // // The program includes a "trial run" of the sorting calls prior to // the timed executions. Java performs some optimizations that // distort the times without these trial runs. import java.util.*; public class Insertion { public static final int SIZE = 5000000; public static void main(String[] args) { // make 2 copies for the comparision (list1, list2) and 2 // copies for the trial runs (list3, list4) int[] list1 = new int[SIZE]; int[] list2 = new int[SIZE]; int[] list3 = new int[SIZE]; int[] list4 = new int[SIZE]; fill(list1, list2, list3, list4); // perform trial runs to warm up the JVM sort(list3); Arrays.sort(list4); runTests(list1, list2); checkMatch(list1, list2); } // make a random list of ints and put in each of the arrays public static void fill(int[] list1, int[] list2, int[] list3, int[] list4) { Random r = new Random(); for (int i = 0; i < SIZE; i++) { int number = r.nextInt(2 * SIZE); list1[i] = number; list2[i] = number; list3[i] = number; list4[i] = number; } } // Runs and times this sort and built-in sort using given lists public static void runTests(int[] list1, int[] list2) { // run and time our sort long start = System.currentTimeMillis(); sort(list1); double elapsed1 = (System.currentTimeMillis() - start)/1000.0; System.out.println("quicksort took " + elapsed1 + " seconds"); System.out.println(); // run and time built-in sort start = System.currentTimeMillis(); Arrays.sort(list2); double elapsed2 = (System.currentTimeMillis() - start)/1000.0; System.out.println("Arrays.sort took " + elapsed2 + " seconds"); System.out.println(); System.out.println("Ratio = " + elapsed1 / elapsed2); } // compares the lists and reports whether they match public static void checkMatch(int[] list1, int[] list2) { boolean match = true; for (int i = 0; i < SIZE; i++) { if (list1[i] != list2[i]) { match = false; } } if (match) { System.out.println("lists match"); } else { System.out.println("lists don't match"); } } // pre : 0 <= index1, index2 < list.length // post: Values of list[x] and list[y] are exchanged. private static void swap(int[] list, int index1, int index2) { int temp = list[index1]; list[index1] = list[index2]; list[index2] = temp; } // sorts the array from indexes low to high inclusive using the insertion // sort algorithm. public static void sort(int[] list, int low, int high) { for (int i = low + 1; i <= high; i++) { int value = list[i]; int spot = Arrays.binarySearch(list, low, i, value); if (spot < 0) { spot = -(spot + 1); } System.arraycopy(list, spot, list, spot + 1, i - spot); list[spot] = value; } } // post: uses insertion sort to sort all elements of the array public static void sort(int[] list) { sort(list, 0, list.length - 1); } }