// Stuart Reges // 12/1/10 // // This program has a method called sort that sorts an array of int values // using the quicksort algorithm. Method main tests it by constructing two // arrays containing random values. It compares the time used by this sort // versus Arrays.sort. import java.util.*; public class Quick { public static void main(String[] args) { int n = 5000000; int[] list1 = new int[n]; int[] list2 = new int[n]; Random r = new Random(); for (int i = 0; i < n; i++) { int number = r.nextInt(2 * n); list1[i] = number; list2[i] = number; } long start = System.currentTimeMillis(); sort(list1); double elapsed1 = (System.currentTimeMillis() - start)/1000.0; System.out.println("quicksort took " + elapsed1 + " seconds"); System.out.println(); 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); boolean match = true; for (int i = 0; i < n; 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; } // post: uses the middle value of the list as a "pivot" to // partition the list into two sublists: all those values less // than or equal to the pivot appearing first followed by the // pivot followed by all those values greater than the pivot; // places the pivot value between the two sublists and returns // the index of the pivot value private static int partition(int[] list, int low, int high) { // swap middle value into first position swap(list, low, (low + high) / 2); // remember pivot int pivot = list[low]; // loop invariant: list divided into 3 parts: values <= pivot followed // by unknown values followed by values > pivot int index1 = low + 1; // index of first unknown value int index2 = high; // index of last unknown value while (index1 <= index2) // while some values still unknown if (list[index1] <= pivot) index1++; else if (list[index2] > pivot) index2--; else { swap(list, index1, index2); index1++; index2--; } // put the pivot value between the two sublists and return its index swap(list, low, index2); return index2; } // post: uses recursive quicksort to sort elements low through // high; base case is a list of 0 or 1 elements, which requires // no sorting; in recursive case, the list is partitioned using // a pivot value and the two resulting lists are recursively sorted public static void sort(int[] list, int low, int high) { if (low < high) { int pivotIndex = partition(list, low, high); sort(list, low, pivotIndex - 1); sort(list, pivotIndex + 1, high); } } // post: uses recursive quicksort to sort all elements of the array public static void sort(int[] list) { sort(list, 0, list.length - 1); } }