File IntegerList.java contains the
IntegerList class from lab 1; IntegerListTest.java contains a simple menu-driven test program that lets the user create,
sort, and print a list and search for an element using sequential or
binary search. (Remember that the list has to be sorted before you can use
binary search!) The only change in this file from lab 1 is that the
binary search method is recursive (binarySearchRec). Study it carefully;
notice that it immediately calls reallyBinarySearchRec, which does all
the work -- exactly what we discussed in class today.
Add a method seqSearchRec to the IntegerList class that does a recursive
sequential search. Now change IntegerListTest.java so that it calls
seqSearchRec instead of seqSearch when the user asks for a sequential
search. Think about how sequential search can be viewed
recursively; if you are looking for an item in a list starting at index i:
- If i exceeds the last index in the list, the item is not found (return -1).
- If the item is at list[i], return i.
- If the is not at list[i], do a sequential search starting at index i+1.
Like binarySearchRec, seqSearchRec will need to set up another method
(e.g., reallySeqSearchRec) to do most of the work. This is because
the recursive method needs more information (the index to start at) than
you want to pass to the top-level search routine, which just needs the thing
to look for.
Print IntegerList.java.