Name: ________________________________
Honor Code Acknowledgment: ___________________
Random Quiz #7
CPS 100, Spring 1997
Due: March 10
Problem 1: Sorting with John Barleycorn
Consider the following code for implementing a merge sort of two linked
lists.
void MergeSort(Node * & list)
// postcondition: list is sorted in non-decreasing order
{
if (list != 0)
{
Node * first;
Node * second;
SplitList(list,first,second);
MergeSort(first);
MergeSort(second);
list = Merge(first,second);
}
}
Part A ( 2 points)
Does list need to be reference parameter? why/why not
Part B ( 2 points)
Write the function prototype/header for the function
SplitList so
that the function MergeSort above will work as intended.
Part C ( 2 points)
If SplitList and Merge execute in
O(n) time for a list of n
elements, what is the complexity
of the implementation of MergeSort above? Why?
Part D ( 2 points)
The code below is the body of an implementation of
SplitList.
Add statement(s) as needed to make it work properly.
{
if (list != 0)
{
first = list;
Node * temp = list;
second = list;
while (temp->next != 0)
{
temp = temp->next->next;
second = second->next;
}
// statement needed here
}
}
Problem 2 : Canine Order (2 points)
True or False: It never makes sense to use an O(n^2)
sort since O(n log n)
sorts exist. Justify your answer (briefly).
Problem 3: Hash, Hash, and more Hash (3 points)
Suppose integers are being inserted into a hashtable of 11 elements.
The integers are hashed using the hash function below.
int Hash(int n)
{
return (n*2) + n/2;
}
The values returned by Hash are converted to indices in
the hash table by using %11 (the remainder after dividing by 11).
Hash collisions are resolved using chaining and new elements are
inserted at the front of each linked list.
Draw a diagram of the hash table that results from inserting
5, 22, 17, 18, 35, 101, 16, 0, 8
into the hash htable in the order shown (5 first, 8 last)
Owen L. Astrachan
Last modified: Mon Oct 27 14:04:36 EST 1997