Code /* Sort2.java CIS 260 David Klick 2005-02-02 Demonstration of various sorting algorithms. Useful sites to visit: http://linux.wku.edu/~lamonml/algor/sort/sort.html http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html */ import java.text.*; public class Sort2 { public static void main(String[] args) { long startTime, elapsedTime; int array_size = 50000; final long RANGE = (long)Integer.MAX_VALUE - Integer.MIN_VALUE + 1; if (args.length > 0) { try { array_size = Integer.parseInt(args[0]); if (array_size < 10) array_size = 10; } catch (Exception e) { // ignore errors } } System.out.println("Sorting an array of " + array_size + " ints"); int[] work = new int[array_size]; int[] hold = new int[array_size]; // create an array of random integers for (int i=0; i a[i+1]) return false; } return true; } public static void mySort(int[] a) { int i, j, temp; int len = a.length; for (i=0; i a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } public static void bubbleSort(int[] a) { int i, j, temp; for (i=a.length-1; i>=0; i--) { for (j = 1; j <= i; j++) { if (a[j-1] > a[j]) { temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; } } } } public static void woldSort(int[] a) { int i, j, k, temp; for (i=0; i a[j]) { temp = a[j]; for (k=j; k>i; k--) a[k] = a[k-1]; a[i] = temp; } } } } public static void selectionSort(int[] a) { int i, j, min, temp; for (i=0; i0) && (a[j-1] > temp)) { a[j] = a[j-1]; j--; } a[j] = temp; } } public static void shellSort(int[] a) { int i, j, incr, temp; incr = 3; while (incr > 0) { for (i=0; i= incr) && (a[j-incr] > temp)) { a[j] = a[j-incr]; j -= incr; } a[j] = temp; } if (incr/2 != 0) incr /= 2; else if (incr == 1) incr = 0; else incr = 1; } } public static void mergeSort(int[] a) { int[] tmp = new int[a.length]; m_sort(a, tmp, 0, a.length-1); } private static void m_sort(int[] a, int[] tmp, int left, int right) { int mid; if (right > left) { mid = (right + left) / 2; m_sort(a, tmp, left, mid); m_sort(a, tmp, mid+1, right); merge(a, tmp, left, mid+1, right); } } private static void merge(int[] a, int[] tmp, int left, int mid, int right) { int i, left_end, num_elements, tmp_pos; left_end = mid - 1; tmp_pos = left; num_elements = right - left + 1; while ((left <= left_end) && (mid <= right)) { if (a[left] <= a[mid]) tmp[tmp_pos++] = a[left++]; else tmp[tmp_pos++] = a[mid++]; } while (left <= left_end) tmp[tmp_pos++] = a[left++]; while (mid <= right) tmp[tmp_pos++] = a[mid++]; for (i=0; i < num_elements; i++) { a[right] = tmp[right]; right--; } } public static void heapSort(int[] a) { int endOfHeap = a.length - 1; for (int i=(endOfHeap-1)/2; i>=0; i--) siftDown(a, i, endOfHeap); do { int tmp = a[0]; a[0] = a[endOfHeap]; a[endOfHeap] = tmp; endOfHeap--; siftDown(a, 0, endOfHeap); } while (endOfHeap > 0); } private static void siftDown(int[] a, int parentNdx, int endOfHeap) { int temp; if (parentNdx == endOfHeap) return; while (parentNdx <= (endOfHeap-1)/2) { int rightChildNdx = parentNdx * 2 + 2; int leftChildNdx = rightChildNdx - 1; int largestChildNdx = leftChildNdx; if ((rightChildNdx <= endOfHeap) && (a[rightChildNdx] > a[leftChildNdx])) { largestChildNdx = rightChildNdx; } if (a[parentNdx] >= a[largestChildNdx]) break; else { temp = a[parentNdx]; a[parentNdx] = a[largestChildNdx]; a[largestChildNdx] = temp; parentNdx = largestChildNdx; } } } public static void quickSort(int[] a) { q_sort(a, 0, a.length-1); } private static void q_sort(int[] a, int left, int right) { int pivot, l_hold, r_hold, temp; l_hold = left; r_hold = right; pivot = a[left]; while (left < right) { while ((a[right] >= pivot) && (left < right)) right--; if (left != right) a[left++] = a[right]; while ((a[left] <= pivot) && (left < right)) left++; if (left != right) a[right--] = a[left]; } a[left] = pivot; temp = left; left = l_hold; right = r_hold; if (left < temp) q_sort(a, left, temp-1); if (right > temp) q_sort(a, temp+1, right); } private static void robSort(int[] a) { while (!isSorted(a)) { for (int i=0; i