<<

Assignment #4: Big-O, Sorting, Tables, History

This assignment is to be done individually and handed in electronically (using submit) before 12:00 noon on Thursday, March 31.

You may submit your answers using plain text, HTML, or Word formats.

1. Consider the following code fragment: for (int pass = 1; pass <= 10; pass++) { for (int index = 0; index < k; index += 10) { for (int count = 0; count < index; count++) { do_something(); } } } Assuming that the execution time of do_something() is independent of k, what is the most accurate description of the worst case run-time for this algorithm? a) O(1) b) O( k ) c) O( k log(k) ) d) O( k 2 ) e) O( k 3 ) Briefly explain your reason for choosing this answer.

2. Consider the following code fragment: for (int pass = 1; pass <= k; pass++) { for (int index = 0; index < 100*k; index += k) { do_something(); } } Assuming that the execution time of do_something() is O(log(k) ), what is the most accurate description of the worst case run-time for this algorithm? a) O( log(k) ) b) O( k ) c) O( k log(k) ) d) O( k 2 ) e) O( k 2 log(k) ) Briefly explain your reason for choosing this answer. 3. Consider the polynomial 10 p 2 + p 3 − 27 p + 100 , and choose the best answer below:

f) This polynomial is O( p 2 ) and not O( p3 ). g) This polynomial is O( p3 ) and not O( p 2 ). h) This polynomial is O( p3 ) and not O( p 4 ). i) This polynomial is O( p 4 ) and not O( p3 ).

4. Consider the following list of 12 symbols and assume that they are in the first 12 locations of array A. k h b m l u t v w r p a a) Assuming the array is sorted using selection sort (p. 388), show its contents after every iteration of the outermost for loop. b) Assuming the array is sorted using linear insertion sort (p. 394), show its contents after every iteration of the outermost for loop. c) Assuming the array is sorted using mergesort (p. 398), show its contents immediately upon returning from every call to merge. d) Assuming the array is sorted using quicksort (p. 409), show its contents immediately upon returning from every call to partition. For the purpose of this question, assume that the subroutine choosePivot() (p. 407) does not rearrange any of the values in the array and that none of the speed-up techniques discussed in class are used to improve performance (that is, the code operates exactly as described on pp. 407-409 in the text).

5. Considering the following list of ten names

Dijkstra, Wirth, Kahn, Hoare, McCarthy, Kay, Dahl, Nygaard, Knuth, Cerf

and assume you start with an initially empty binary search tree. Answer the following questions based on the final binary search tree (p. 517) that results from inserting the list of ten names in the order given. a) What is the height of this final tree? b) How many nodes in the final tree have two empty subtrees? c) Assuming that the root has level number 1, on which level does the with value Hoare occur? d) Is either subtree of the root node an empty tree? e) Which value is in the node that is the parent of the node containing McCarthy? f) Is the value Nygaard in the right subtree of the tree that has Dahl as its root value? 6. Match each of the names in the first column to a contribution made by that named person chosen from the right column. You may wish to search on the Web or check Prof. Shallit’s history page at http://www.cs.uwaterloo.ca/~shallit/Courses/134/history.html

a) (A) invented the QuickSort algorithm b) (B) showed that for any algorithmic system, there are c) some provably true or provably false statements that cannot be decided using only the tools available in d) Edgar Codd that system e) (C) with Bob Kahn co-invented TCP/IP, the f) Doug Englebart communications protocol for the g) George Forsythe (D) with J. Presper Eckert, co-invented the ENIAC h) (E) predicted that technology would continue to improve i) so that the number of transistors that can fit on a j) Herman Hollerith computer chip would double within every 2-year k) period l) John Mauchly (F) designed a mechanical digital computer, which could not be built due to the inadequacies of manufacturing m) Gordon Moore technology at the time n) (G) invented the Pascal programming language o) (H) invented the mouse p) (I) strongly influenced the design and standardization of the Cobol computer language (J) created the first working programmable computer (K) pioneered object-oriented programming and personal computing (L) showed that there is a large class of problems that can be solved in polynomial time if and only if all problems in a much larger class can also be solved in polynomial time (M) invented the punched card reader (N) spearheaded the invention of the Fortran programming language (O) instrumental in formulating the discipline of (P) invented the relational database model In your own words, write a paragraph elaborating on the contributions of any one of these people. Note: Late assignments will be accepted until Monday, April 4, at 12:00 noon.