Lecture Notes on 30 July 2014 // method that does selection sort public static void selectionSort (int[] a) { for (int i = 0; i < a.length - 1; i++) { // find the minimum int min = a[i]; int minIdx = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < min) { min = a[j]; minIdx = j; } } // swap the elements a[minIdx] = a[i]; a[i] = min; } } // method that does sequential search public static int seqSearch (int[] a, int x) { for (int i = 0; i < a.length; i++) { if (x == a[i]) return i; } return -1; } // method does binary search public static int binarySearch (int[] a, int x) { int lo = 0; int hi = a.length - 1; int mid; while (lo <= high) { mid = (lo + hi) / 2; if (x > a[mid]) lo = mid + 1; else if (x < a[mid]) hi = mid - 1; else return mid; } return -1; } // method merges two sorted arrays public static int[] merge (int[] a, int[] b) { // create an array to store the merged values int[] c = new int [a.length + b.length]; int idxA = 0; int idxB = 0; int idxC = 0; while ((idxA < a.length) && (idxB < b.length)) { if (a[idxA] < b[idxB]) { c[idxC++] = a[idxA++]; } else { c[idxC++] = b[idxB++]; } } while (idxA < a.length) { c[idxC++] = a[idxA++]; } while (idxB < b.length) { c[idxC++] = b[idxB++]; } return c; }