Timing Searching and Sorting Algorithms
Chapter 10 has a brief discussion comparing sorting algorithms and searching algorithms. In this exercise you
will use an IntegerList class (in the file IntegerList.java) and a driver (in the file IntegerListTest.java) to
examine the runtimes of the searching and sorting algorithms. The IntegerListTest class has several options for
creating a list of a given size, filling the list with random integers or with already sorted integers, and searching
or sorting the list. (NOTE: You may have used a version of these classes in a previous lab.) Add the methods
minIndex, swap, linearSearch, and binarySearch to the IntegerList class (see the calls to determine the
parameters). Run IntegerListTest a few times to explore the options.
The runtimes of the sorting and searching algorithms can be examined using the Java method
System.currentTimeMillis(), which returns the current system time in milliseconds. (Note that it returns a long,
not an int.) You will have to import java.util.* to have access to this method. In IntegerListTest, just get the
system time immediately before and immediately after you perform any of the searches or sorts. Then subtract
the first from the second, and you have the time required for the operation in milliseconds. WARNING: Be sure
you are not including any input or output in your timed operations; these are very expensive and will swamp
your algorithm times!
Add appropriate calls to System.currentTimeMillis() to your program, run it and fill out the tables below. Note
that you will use much larger arrays for the search algorithms than for the sort algorithms; do you see why?