Loyola College in Maryland

CS 301 - Data Structures and Algorithms I
Fall 2003


Loyola College > Department of Computer Science > CS 301 > Homework > Homework 5

Due

Wednesday, September 24 at the beginnging of class

Problems

1. Write a client function that merges two instances of the Sorted List ADT using the following specification (this function is not a member of SortedList type so it does not have access to private variables.
MergeLists(SortedType list1, SortedType list2, SortedType& result)
Function: Merges two sorted lists into a third sorted list.
Preconditions: list1 and list2 have been initialized and are sorted by key using function ComparedTo.
Postconditions: result is sorted list that contains all of the items from list1 and list2.
a. Write the prototype for MergeLists in pseudocode.
b. Write the code for the function.

2. Imagine that you have 101 dalmations; no two dalmations have the same number of spots. Suppose that you create an array of 101 integers: The first integer is the number of spots on the first dalmation, the second integer is the number of spots on the second dalmation, and so on.
Your friend wants to know whether you have a dalmation with 99 spots. Thus, you need to determine whether that array contains the integer 99.
a. If you plan to use a binary search to look for the 99, what, if anything, would you do to the array before searching it?
b. What is the index of the integer in the array that a binary search would examine first?
c. If all your dalmations have more than 99 spots, exactly how many comparisons will a binary search require to determine 99 is not in the array?