NAME: ______

SCORE: ______

MONTANA STATE UNIVERSITY

Final Examination

May 6, 2003

CS 221: Computer Science I

Time: 110 minutes

(Students are encouraged to read through the questions carefully at the beginning of the exam)

NO CALCULATORS ALLOWED

QUESTION AND ANSWER BOOKS MAY NOT BE REMOVED FROM THE EXAMINATION ROOM

Total Points: 100 Total Questions: 6 Total Pages: 10

Students should attempt all questions.

This exam contributes 25% of the grade toward this course. 1. (15 points.) Sorting. Consider the code below.

[a] Name the sorting technique that is being used.

[b] Assume that when sort is called, the data array contains 5 elements in the following order: 200, 400, 500, 300, 100. Show the contents of data in statement 1 when i first takes on the value 4, then 3, then 2, then 1, then 0. public void sort () { for (int i = data.length -1; i > 0; i--) // statement 1 { int largestIndex = i;

for (int j = 0; j < i; j++) // statement 2 { if (data[j] > data[largestIndex]) { largestIndex = j; // statement 3 } } int temp = data[largestIndex]; // statement 4 data[largestIndex] = data[i]; data[i] = temp; } } private int data []; 2. (15 points.) Statement counts. Assume that the data array in question 1 contains n elements.

[a] What is the statement count for statement 1?

[b] What is the statement count for statement 2? Show your work.

[c] Describe in English the situation that leads to the best case statement count for statement 3.

[d] What is the best case statement count for statement 3?

[e] Describe in English the situation that leads to the worst case statement count for statement 3.

[f] What is the worst case statement count for statement 3?

[g] What is the statement count for statement 4? 3. (15 points.) Time Complexity. Assume that method doThis has a best case statement count of 10n + 17 and a worst case statement count of 54 + 21logn + 712n. Assume that the method doThat has a worst case statement count of 4n2 + 3n + 2 and a best case statement count of 17n + 23.

[a] Fill in the blank with the most appropriate time complexity function. The possible functions are o (write “little o” instead so that I can distinguish this from O), O (write “big O”), Θ, Ω, and ω.

doThis is ______( 1 )

[b] doThis is ______( log n)

[c] doThis is ______( n )

[d] doThis is ______( n log n )

[e] doThis is ______( n2 )

[f] doThat is ______( 1 )

[g] doThat is ______( log n)

[h] doThat is ______( n )

[i] doThat is ______( n log n)

[j] doThat is ______( n2 )

[k] Prove that doThis has a bad lower bound of 1. 4. (15 points.) Recursion. Solve the following problem recursively. Method countVowels should return the number of vowels that are contained in the character array, word. You may assume that all characters contained in word are lower case. For example, if word is initialized to {‘r’, ‘e’, ‘c’, ‘u’, ‘r’, ‘s’, ‘i’, ‘o’, ‘n’} before countVowels is called, the method should return 4. For the purpose of this problem, assume that ‘y’ is not a vowel. public int countVowels (char word[]) 5. (15 points.) To perform a mergesort, one must be able to merge two sorted arrays. Complete the method named merge below. When merge is first called, you may assume that both array1 and array2 are in descending sorted order. When merge is finished, it should return an array that contains the merged contents of both array1 and array2 in descending sorted order. For example, if array1 contained [10, 8, 6] and array2 contained [9, 8, 7, 6], then [10, 9, 8, 8, 7, 6, 6] would be returned. public int [] merge (int array1[], int array2[]) Use this page to continue your answer for question 5 if you need the extra space. 6. (25 points.) The Deque ADT. Consider the interface below. Implement the methods below in a class called DequeVector. Include the class constructor. Use the built-in java.util.Vector class as the underlying data structure. public interface Deque { public void insertFirst (Object element); public Object removeLast () throws Exception; public boolean isEmpty (); public boolean isFull (); public int size (); } Use this page to continue your answer for question 6 if you need the extra space. This page is not used. Good luck with your remaining finals. Have a great summer!