Name: ________________________________

Honor Code Acknowledgment: ___________________


Random Quiz # 10

CPS 100, Spring 1996

Due: February 23


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 poinst)

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).