Lab 6 Exception Handling, Searching and Sorting Student Name: ______________________________________________________ TA Initial and Checkout time: ___________________________________________ TA Comments: ______________________________________________________ Prelab: Answer the following questions before working on the lab computers. CSci112 Fall 2011 Closed Lab Handout! Lab 6, Friday, October 7, 2011 http://www.cs.olemiss.edu/~jxue/teaching/csci112_F11/notes/lab6 Prepared by Jianxia Xue, Last Update Tuesday, October 4, 2011! 1 of 3 1. In the worst case, how many comparisons does binary search take to find a target item given a data pool size of n? __________________________ __________________________ 2. Does selection sort perform faster on a sorted vs an inversely sorted pool? What about bubble sort? __________________________ __________________________ __________________________ __________________________ 3. What is the Java statement to catch and process an exception? Is it possible to process two different types of exceptions separately? Is it possible to process 4 different types of exceptions all at once? __________________________ __________________________ __________________________ __________________________ 4. What is the Java statement to explicitly generate an exception? __________________________ __________________________ __________________________ __________________________ Coding Tasks 1. Debug BuggyBinarySearch.java The given code of BuggyBinarySearch.java has two syntax errors distributed inside the binarySearch and the main methods. You can fixed them all together at one place inside the code, the eclipseʼs hint system does not provide such an option for you. Apply your knowledge on checked vs. unchecked Java exceptions to find a solution smarter than eclipseʼs suggestions. Once the syntax errors are fixed, the driver program can be run. It tries to search a user input word from a hard coded string array. It works for some entries, but not all. For example, it can find the word “try”, but not “sort”. The binarySearch method is a correct implementation. Whatʼs wrong with the code? 2. Develop Sorting.java and evaluate selection and bubble sort algorithms In the following bubble sort implementation: for (int i = n-1; i >= 0; i--) for (int j = 0; j <= i-1; j++) if (pool[j].compareTo(pool[j + 1]) > 0) swap(pool, j, j + 1); the algorithm tries to swap larger items towards the end of the unsorted set. Such behavior can be referred as the bubble-down sort. In this task we will make some variations from the given version. a) Implement the bubble-up sort, in which smaller items will be bubbled up towards the beginning of the unsorted set? Run the existing driver method to validate your implementation of the bubbleUp method first. b) In your bubble-up implementation, add code so every time the bubbleUp method is activated, one can see how many times the compareTo method was called, and how many times the swap method was called. c) Add code to the existing selection sort method to report how many times the methods compareTo and swap are called inside the selection sort. d) In the main method, the existing code allow you to compare the performance between selection and bubble sort in execution time. With added code from part b) and c), you can also compare the two sorting algorithm in terms of the number of compares and swaps. Run the program with 10, 100, and 1000 entries and check the number of compares and swaps in the selection and the bubble sorts. e) Upgrade the main method to compare the two sorting algorithms in their best- case and worst-case performances. Note that you can use the method of reverse to get the worst case arrays. CSci112 Fall 2011 Closed Lab Handout! Lab 6, Friday, October 7, 2011 http://www.cs.olemiss.edu/~jxue/teaching/csci112_F11/notes/lab6 Prepared by Jianxia Xue, Last Update Tuesday, October 4, 2011! 2 of 3 3. (Extra Credit) Sort application Can you make use the sorting algorithm to generate a set from a given array? A set is a collection of items with no duplicate. For example, given an input array of {1,2, 4, 2, 1, 5, 2, 4, 1, 5}, the corresponding set is {1, 2, 4, 5}. Store the resulting set in an array with matched size, and make your implementation polymorphable so we can apply the method for vocabulary generation given an array of random words. CSci112 Fall 2011 Closed Lab Handout! Lab 6, Friday, October 7, 2011 http://www.cs.olemiss.edu/~jxue/teaching/csci112_F11/notes/lab6 Prepared by Jianxia Xue, Last Update Tuesday, October 4, 2011! 3 of 3