CS 121: Assignment 9

Searching, Linked Lists

Due: Monday, March 31, 10:00 am

20% each.

1) On paper, perform a binary search for the value 17 on the following array. For each recursive step, state the range of indices that is being searched.

(-20, -8, -1, 0, 3, 4, 15, 16, 17, 21)

2) Write two methods that test the binary search method from the book. The first one creates a sorted array, then searches for an item that is in the array, and checks that the result is correct. The second one searches for an item that is not in the array, and checks that the result is correct. Hint: to check that an item is not in the array, one has to do a linear search.

3) Turn the test methods from (2) into automated unit tests. To do so, you have to create a class called BinarySearchTestCase that extends the TestCase class from the JUnit library. In this class, you need to create a setup() method. Remember that the name of each test method must start with the word "test". Use the assertEquals() and assertTrue() methods to check whether your test succeeded. Make sure your tests run correctly in Eclipse with the command "Run as JUnit test".

4) Exercise P15.1 (page 693).

5) Use the StopWatch class to measure the time needed for inserting an element at the beginning of an ArrayList of 1 million elements. Compare it to the time needed to do the same with a LinkedList. Turn in your code as well as your measurements.



CS 121