// Sort.java // // CS 101 Class Example // // Java methods for sorting of arrays of integers import java.util.*; // for Random number generator public class Sort { // --------- Main program--------------------------------------------- public static void main (String[] args) { int n = 10; // default array size if (args.length > 0) { // if provided, n = Integer.parseInt(args[0]); // get array size from command line } int[] a = new int[n]; int[] b = new int[n]; initArr(a); // initialize array with random numbers System.out.println(); System.out.print("Initial values: "); printArr(a); System.out.println(); copyArr(a, b); selectionSort(b); System.out.print("Selection Sort: "); printArr(b); copyArr(a, b); insertionSort(b); System.out.print("Insertion Sort: "); printArr(b); copyArr(a, b); bubbleSort(b); System.out.print("Bubble Sort: "); printArr(b); copyArr(a, b); mergeSort(b); System.out.print("Merge Sort: "); printArr(b); copyArr(a, b); quickSort(b); System.out.print("Quick Sort: "); printArr(b); } // --------- Selection Sort ------------------------------------------ public static int minIndexBetween (int[] a, int lo, int hi) { // Return the index of the minimum integer in a[lo..hi] int minVal = a[lo]; int minInd = lo; for (int i = lo+1; i <= hi; i++) { if (a[i] < minVal) { minVal = a[i]; minInd = i; } } return minInd; } public static void selectionSort (int[] a) { for (int i = 0; i < a.length-1; i++) { int j = minIndexBetween(a, i, a.length-1); if (i != j) { swap(a, i, j); } } } // --------- Insertion Sort ------------------------------------------ public static void insertionSort (int[] a) { for (int i = 1; i < a.length; i++) { insertIth(a, i); } } public static void insertIth(int[] a, int i) { // Assume a[0..i-1] is sorted. // Insert a[i] into a[0..i] in such a way to make it sorted. int val = a[i]; int k = i-1; while ((k >= 0) && (a[k] > val)) { // Order of tests is crucial! a[k+1] = a[k]; // Shift a[k] to the right k--; } // At this point, either (k == -1) or ((k >=0 ) && (a[k] <= val)) a[k+1] = val; } // --------- Bubble Sort --------------------------------------------- public static void bubbleSort (int[] a) { for (int i = a.length-1; i > 0; i--) { for (int j = 0; j < i; j++) { if (a[j] > a[j+1]) { swap(a, j, j+1); } } } } // --------- Merge Sort ---------------------------------------------- public static void mergeSort(int[] a) { int[] b = new int[a.length]; // temporary array mergeSortR(a, b, 0, a.length-1); } public static void mergeSortR(int[] data, int[] temp, int lo, int hi) { if (hi-lo >= 1) { // only need to sort if at least 2 elements int mid = (hi + lo + 1) / 2; for (int i = lo; i < mid; i++) { // move low half into temp storage temp[i] = data[i]; } mergeSortR(temp, data, lo, mid-1); // sort lower half mergeSortR(data, temp, mid, hi); // sort upper half merge(data, temp, lo, mid, hi); // merge halves together } } public static void merge(int[] data, int[] temp, int lo, int mid, int hi) { int ri = lo; // result index int ti = lo; // temp index int di = mid; // data index // while two lists are not empty merge smaller value while (ti < mid && di <= hi) { if (data[di] < temp[ti]) { data[ri++] = data[di++]; // smaller is in high data } else { data[ri++] = temp[ti++]; // smaller is in temp } } // possibly some values left in temp array while (ti < mid) { data[ri++] = temp[ti++]; } // ...or possibly some values left (in correct place) in data array } // --------- Quick Sort ---------------------------------------------- public static void quickSort(int[] a) { quickSortR(a, 0, a.length-1); } public static void quickSortR(int[] a, int lo, int hi) { int i = lo; int j = hi; int pivot = a[lo]; while (i <= j) { while (a[i] < pivot && i < hi) { i++; } while (pivot < a[j] && j > lo) { j--; } if (i <= j) { swap(a, i, j); i++; j--; } } if (lo < j) { quickSortR(a, lo, j); } if (i < hi) { quickSortR(a, i, hi); } } // --------- Helper Methods ------------------------------------------ public static void initArr(int[] a) { // initialize array a with random numbers between 0 and 99 Random rand = new Random(); for (int i = 0; i < a.length; i++) { a[i] = Math.abs(rand.nextInt()) % 100; } } public static void swap (int[] a, int i, int j) { // Swap a[i] and a[j] int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } public static void printArr(int[] a) { // print array a for (int i=0; i < a.length; i++) { System.out.print(a[i] + " "); // print all elements on same line } System.out.println(); // print newline } public static void copyArr(int[] src, int[] dst) { // copy array src to array dst if (src.length != dst.length) { throw new RuntimeException("Sort.copyArr: arrays must have same length!"); } else { for (int i=0; i < dst.length; i++) { dst[i] = src[i]; } } } }