import java.util.*; public class SearchSortIncomplete { public static boolean debug = false; public static Scanner sc = new Scanner (System.in); public static void main(String[] args) { // a value to search for, hoping for its index in the array. int key = 0; // a resulting index to be returned from a search. int result = 0; //does the user want to quit? boolean quit = false; //change this however you like. int[] x = { 3, 5, 2, 7, 4, 6, 0, 10, 3 }; int[] copyOfX = new int[x.length]; //why can't we just say "copyOfX = x;" ? for (int i = 0; i < x.length; i++) { copyOfX[i] = x[i]; } while (!quit) { printMenu(); //get a number from 1-9 from the user. int choice = getChoice(9); //just these options need an extra value, for the key. if (choice == 5 || choice == 6) { key = getChoice(); } //dispatch the requested operation. switch (choice) { case 1: bubbleSort(x); break; case 2: insertionSort(x); break; case 3: mergeSort(x); break; case 4: quickSort(x); break; case 5: result = linearSearch(key, x); displaySearchResult(result, key); break; case 6: result = binarySearch(key, x); displaySearchResult(result, key); break; case 7: printArray(x); break; case 8: for (int i = 0; i < x.length; i++) { x[i] = copyOfX[i]; } printArray(x); break; case 9: quit = true; break; default: System.out.println("try again!"); } } } /* Sorting Methods Section. */ public static void bubbleSort(int[] a) { //TODO: implement! } public static void insertionSort(int[] a) { //TODO: implement! } public static void mergeSort(int[] a) { //TODO, for the mighty: implement! } public static void quickSort(int[] a) { quickSort(a, 0, a.length - 1); } public static void quickSort(int[] a, int leftIndex, int rightIndex) { //TODO, for the mighty: implement! } /** * Searching Methods Section. */ public static int linearSearch(int k, int[] a) { //TODO: implement! return -1; } public static int binarySearch(int k, int[] a) { return binarySearch(k, a, 0, a.length - 1); } private static int binarySearch(int k, int[] a, int left, int right) { //TODO: implement! return -1; } /** * Helpful Functions Section. These all make the main method more * "streamlined." */ public static void printArray(int[] a) { //a slightly smart printing function. String vals = "values:"; String indices = "index: "; for (int i = 0; i < a.length; i++) { if (a[i] < 10) vals += " "; if (a[i] < 100) vals += " "; if (a[i] >= 0) vals += " "; vals += a[i]; if (i < 10) indices += " "; if (i < 100) indices += " "; indices += " " + i; } System.out.println(vals + "\n" + indices); } public static int getChoice(int num) { int x = -1; while (x < 1 || x > num) { try { x = sc.nextInt(); } catch (Exception e) { System.out .println("Whoops! try again--enter a menu option from 1 to " + num + ":"); sc.nextLine(); } } return x; } public static int getChoice() { System.out.println("please enter a number:"); while (true) { try { return sc.nextInt(); } catch (Exception e) { System.out.println("Whoops! try again--enter a number:"); sc.nextLine(); } } } public static void printMenu() { System.out.println("Please choose:"); System.out.println("[1]Bubble Sort"); System.out.println("[2]Insertion Sort"); System.out.println("[3]Merge Sort"); System.out.println("[4]Quick Sort"); System.out.println("[5]Linear Search"); System.out.println("[6]Binary Search"); System.out.println("[7]Print the Array"); System.out.println("[8]Reset the Array"); System.out.println("[9]Quit!"); } public static void displaySearchResult(int r, int k) { //display results for if (r != -1) { System.out.println("The index for key=" + k + " is " + r); } else { System.out.println("The key=" + k + " was not found in the array."); } } }