<<

Assignment #4: Big−O, Sorting, Tables, History − Soln

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

2 d) O(k ) ←

3 e) O(k ) Briefly explain your reason for choosing this answer. The outer loop executes (10+1-1)/1=10 times, and the middle loop executes (k-0)/10 = k/10 times for each execution of the outer loop. The innermost loop executes no times at first, then once, then twice, until k-1 times the last time that the middle loop executes, and the body runs O(1) time. Thus the run time is 10*(0+1+2+?+k- 1)*O(1) = O(k2).

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?

f) O(log(k) ) g) O(k )

h) O(k log(k) ) ←

2 i) O(k ) 2 j) O(k log(k) ) Briefly explain your reason for choosing this answer. The outer loop is executed (k+1-1)/1 = k times, the inner loop is exectuted (100k-0)/k = 100 times, and the innermost block takes time O(log(k)). Thus the overall time is k*100*log(k) = O(k log(k)).

2 + 3 − + 3. Consider the polynomial 10p p 27 p 100, and choose the best answer below:

2 3 k) This polynomial is O( p ) and not O( p ).

3 2 l) This polynomial is O( p ) and not O( p ). ←

3 4 m) This polynomial is O( p ) and not O( p ).

4 3 n) This polynomial is O( p ) and not O( p ).

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 o) Assuming the array is sorted using selection sort (p. 388), show its contents after every iteration of the outermost for loop. k h b m l u t v a r p w (for convenience, moved elements in bold) k h b m l u t p a r v w k h b m l r t p a u v w k h b m l r a p t u v w k h b m l p a r t u v w k h b m l a p r t u v w k h b a l m p r t u v w k h b a l m p r t u v w a h b k l m p r t u v w a b h k l m p r t u v w a b h k l m p r t u v w p) Assuming the array is sorted using linear insertion sort (p. 394), show its contents after every iteration of the outermost for loop. h k b m l u t v w r p a b h k m l u t v w r p a b h k m l u t v w r p a b h k l m u t v w r p a b h k l m u t v w r p a b h k l m t u v w r p a b h k l m t u v w r p a b h k l m u t v w r p a b h k l m r u t v w p a b h k l m p r u t v w a a b h k l m p r u t v w q) Assuming the array is sorted using mergesort (p. 398), show its contents immediately upon returning from every call to merge. h k b m l u t v w r p a b h k m l u t v w r p a b h k l m u t v w r p a b h k l m u t v w r p a b h k l m u t v w r p a b h k l m u t v w r p a b h k l m u t v w r p a b h k l m u t v w p r a b h k l m u t v w a p r b h k l m u a p r t v w a b h k l m p r u t v w r) 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). Assuming the algorithm given in the text: a h b k l u t v w r p m a h b k l u t v w r p m a b h k l u t v w r p m a b h k l u t v w r p m a b h k l m t r p u w v a b h k l m t r p u w v a b h k l m p t r u w v a b h k l m p r t u v w Alternatively, assuming the algorithm given in the movie: a h b k l u t v w r p m a h b k l u t v w r p m a b h k l u t v w r p m a b h k l u t v w r p m a b h k l r t m p u w v a b h k l m p r t u w v a b h k l m p r t u v w

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. s) What is the height of this final tree? 6 t) How many nodes in the final tree have two empty subtrees? 4 u) Assuming that the root has level number 1, on which level does the with value Hoare occur? 4 v) Is either subtree of the root node an empty tree? no w) Which value is in the node that is the parent of the node containing McCarthy? Kahn x) Is the value Nygaard in the right subtree of the tree that has Dahl as its root value? No y) F (A) invented the QuickSort algorithm z) N (B) showed that for any algorithmic system, there are aa) C some provably true or provably false statements that cannot be decided using only the tools available in bb) Edgar Codd P that system cc) L (C) with Bob Kahn co−invented TCP/IP, the dd) Doug Englebart H communications protocol for the ee) George Forsythe O (D) with J. Presper Eckert, co−invented the ENIAC ff) A (E) predicted that technology would continue to improve so that the number of transistors that can fit on a I gg) computer chip would double within every 2−year hh) Herman Hollerith period M (F) designed a mechanical digital computer, which could ii) K not be built due to the inadequacies of manufacturing technology at the time jj) John Mauchly D (G) invented the Pascal programming language kk) Gordon Moore E (H) invented the mouse ll) B (I) strongly influenced the design and standardization of mm) G the Cobol computer language nn) J (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