CS 360 Exam 1 Fall 2014 Name 1. Answer the following questions about each code fragment below. [8 points] for (v=1; v<=n; v++) for (w=1; w<=v; w++) for (x=v; x<=n; x++) for (y=w; y<=x; y++) z++; for (r=1; r<=n; r++) for (s=n+1; s<=2*n; s++) for (t=r; t<=s; t++) u++; If n=3, how many total times is z incremented? If n=2, how many total times is z incremented? If n=2, how many total times is u incremented? If n=3, how many total times is u incremented? class A { int F( ) { return 3; } int G( ) { return 3 + F( ); } int H( ) { return 6 + G( ); } } class B extends A { int H( ) { return 7 + G( ); } } class C extends B { int G( ) { return 5 + F( ); } } class D extends C { int F( ) { return 6; } } class E extends D { int H( ) { return 9 + G( ); } } A b, c, d, e; b = new B( ); b.h( ); c = new C( ); c.h( ); d = new D( ); d.h( ); e = new E( ); e.h( ); This code is object-oriented. Therefore it uses both inheritance and late method binding (also known as dynamic method binding or dynamic dispatch). What value is returned from the call b.h( )? What value is returned from the call c.h( )? What value is returned from the call d.h( )? What value is returned from the call e.h( )?
2. Evaluate each expression and write the answer in simplest form. [12 points] 100 mod 13 = 13 mod 100 = 7777 9 mod 25 = 3 lg 256 = lg 80 + lg 48 lg 30 = 100 mod 25 = 200 11 k=0 = k=100 k = k=0 2kk = 4 kk log3 243 = log10 0.001 = log16 2 = 3. Questions about running times. [4 points] First suppose we run a program, and we find that T(2) = 0.01 seconds and T(8) = 40.96 seconds. a. If T(n) = c k n, determine the value of constant k. b. If T(n) = c n k, determine the value of constant k. Next we run a different program using an input of size 100, and we find that T(100) = 5 seconds. c. If T(n) = c n 3 for some constant c, then determine T(200). d. If T(n) = c n for some constant c, determine the value of n0 such that T(n0) = 15 seconds.
4. Suppose f(n) and g(n) are related by at least one of the relations O, o, θ, Ω, ω. Match each condition below to its equivalent relation. [13 points] A. f(n) is O(g(n)) B. f(n) is o(g(n)) C. f(n) is θ(g(n)) D. f(n) is Ω(g(n)) E. f(n) is ω(g(n)) F. Contradictory or impossible situation f(n) is θ(g(n)) or f(n) is not Ω(g(n)). f(n) is not o(g(n)) and f(n) is not ω(g(n)). f(n) is Ο(g(n)) and f(n) is ω(g(n)). f(n) is Ο(g(n)) and f(n) is Ω(g(n)). f(n) is Ω(g(n)) and f(n) is o(g(n)). f(n) is θ(g(n)) or f(n) is o(g(n)). f(n) is θ(g(n)) or f(n) is ω(g(n)). f(n) is not θ(g(n)) and f(n) is not o(g(n)). f(n) is not θ(g(n)) and f(n) is not ω(g(n)). f(n) is not Ο(g(n)) and f(n) is not Ω(g(n)). f(n) is not O(g(n)). f(n) is not Ω(g(n)). f(n) is θ(g(n)) or f(n) is not Ο(g(n)).
5. State the running time of each code fragment as a simplest θ function of n. [9 points] for (k=1; k<=n*n*n; k++) for (k=1; k*k*k <=n; k++) for (k=2; k<=n; k*=k) for (k=1; k<=n; k+=k) for (j=1; j<=n; j++) if (j>100 && j<n 100) for (k=1; k<=n; k++) print (j, k); for (j=1; j<=n; j++) if (j<100 j>n 100) for (k=1; k<=n; k++) print (j, k); m=1; for (j=1; j<=n; j++) m*=m; for (k=1; k<=m; k++) m=2; for (j=1; j<=n; j++) m*=m; for (k=1; k<=m; k++) x=0; y=1; for (z=1; z<=n; x+=6, y+=x, z+=y) print (z);
6. Questions about divide-and-conquer recurrences. [9 points] Solve each recurrence, and write the solution as a simplest θ function of n: a. T(n) = 625 T(n/5) + n 4. b. T(n) = 1000 T(n/10) + n 4. c. T(n) = 32 T(n/2) + n 4. Toom-k is a divide-and-conquer algorithm for polynomial multiplication that works as follows: Split each n-term polynomial into k pieces that each have n/k terms. Perform 2k 1 recursive multiplications, along with a constant number of additions and/or subtractions on polynomials that each have n/k terms. d. When k=3, we have Toom-3 or Toom-Cook. Write a recurrence for T(n), and also solve for T(n). e. Next consider Toom-k with arbitrary k. Write a recurrence for T(n), and also solve for T(n). f. Show that for each ε>0 there exist values of k such that Toom-k has running time o(n 1+ε ). Also determine such values of k expressed in terms of ε.
7. Use the array [9, 5, 8, 4, 7, 3, 6, 2] as input for each sorting algorithm below. [6 points] Which choice shows the array contents after two passes of insertion sort? Which choice shows the array contents after four passes of insertion sort? Which choice shows the array contents after two passes of selection sort? Which choice shows the array contents after four passes of selection sort? A. [5, 8, 9, 4, 7, 3, 6, 2] B. [4, 3, 5, 2, 6, 7, 8, 9] C. [4, 5, 3, 6, 2, 7, 8, 9] D. [2, 3, 8, 4, 7, 5, 6, 9] E. [2, 3, 4, 5, 7, 8, 6, 9] F. [4, 5, 8, 9, 7, 3, 6, 2] G. [5, 4, 7, 3, 6, 2, 8, 9] H. [4, 5, 7, 8, 9, 3, 6, 2] I. [9, 5, 8, 4, 2, 3, 6, 7] J. [2, 3, 4, 8, 7, 5, 6, 9] K. None of the above Which choice shows the array contents after two passes of bubble sort? Which choice shows the array contents after four passes of bubble sort? 8. Use the input array [237, 564, 267, 534, 567, 234, 537, 264]. [4 points] Which choice shows the array after the first iteration of an (ascending-order) radix sort using radix r=10? Which choice shows the array after the second iteration of an (ascending-order) radix sort using radix r=10? Which choice shows the array after the first iteration of a descending-order radix sort using radix r=10? Which choice shows the array after the second iteration of a descending-order radix sort using radix r=10? A. [264, 537, 234, 567, 534, 267, 564, 237] B. [237, 267, 567, 537, 564, 534, 234, 264] C. [564, 534, 234, 264, 237, 267, 567, 537] D. [267, 567, 564, 264, 237, 537, 534, 234] E. [534, 234, 237, 537, 564, 264, 267, 567] F. [237, 267, 234, 264, 564, 534, 567, 537] G. [237, 234, 534, 537, 267, 264, 564, 567] H. [564, 534, 567, 537, 237, 267, 234, 264] I. [564, 567, 267, 264, 534, 537, 237, 234] J. [234, 237, 264, 267, 534, 537, 564, 567] K. [567, 564, 537, 534, 267, 264, 237, 234] L. None of the above
9. Use the array [7, 5, 8, 4, 1, 6, 2, 0, 3] as input for each sorting algorithm below. [7 points] Which choice shows the two subarray parameters sent to the two recursive calls of merge sort? Which choice shows the two subarrays returned from the two recursive calls of merge sort? For the following, choose the pivot using median-of- 3 with the low, middle, and high indexes. What is the pivot value? Which choice shows the two subarray parameters sent to the two recursive calls of (not-in-place) quick sort? A. [0, 1, 2, 3] and [4, 5, 6, 7, 8] B. [0, 1, 2, 3] and [5, 6, 7, 8] C. [1, 2, 0] and [7, 5, 8, 4, 6] D. [4, 5, 7, 8] and [0, 1, 2, 3, 6] E. [7, 5, 8, 4, 1] and [6, 2, 0, 3] F. [3, 0, 2, 1] and [4, 6, 8, 5, 7] G. [1, 4, 5, 7, 8] and [0, 2, 3, 6] H. [0, 1, 2, 3, 4] and [5, 6, 7, 8] I. [7, 5, 8, 4] and [1, 6, 2, 0, 3] J. [0, 1, 2] and [4, 5, 6, 7, 8] K. None of the above Which choice shows the two subarrays returned from the two recursive calls of (not-in-place) quick sort? Which choice shows the two subarray parameters sent to the two recursive calls of in-place quick sort? Which choice shows the two subarrays returned from the two recursive calls of in-place quick sort? 10. Questions about sorting algorithms. [4 points] Which algorithm has θ(n lg n) worst-case running time and is also stable? Which algorithm has θ(n lg n) worst-case running time and is also in-place? Which two algorithms are simultaneously both stable and in-place? A. Heap sort B. Counting sort C. Bin sort D. Insertion sort E. Selection sort F. Bubble sort G. Merge sort H. Quick sort I. Radix sort J. None of the above
11. Use the input array [5, 7, 3, 6, 8, 4, 2]. [4 points] Which choice shows the result after inserting each given array element (in the given order) into an array-based minordered binary heap? Which choice shows the result after inserting each given array element (in the given order) into an array-based maxordered binary heap? Which choice shows the result after applying Build-Min- Heap to the given input array? Which choice shows the result after applying Build-Max- Heap to the given input array? A. [2, 4, 8, 6, 3, 7, 5] B. [2, 3, 4, 5, 6, 7, 8] C. [8, 7, 6, 5, 4, 3, 2] D. [2, 6, 3, 7, 8, 4, 5] E. [2, 6, 3, 7, 8, 5, 4] F. [8, 7, 4, 6, 5, 3, 2] G. [8, 7, 4, 5, 6, 3, 2] H. [2, 3, 6, 4, 5, 7, 8] I. [2, 3, 6, 5, 4, 7, 8] J. [8, 4, 7, 3, 2, 6, 5] K. [8, 4, 7, 3, 2, 5, 6] L. None of the above
12. Suppose you are given a sorted list of n elements followed by f(n) additional randomly ordered elements. Make no assumptions about the type or range of the elements. [10 points] a. Explain how to sort the entire list in O(n) time when f(n) is O(1). Justify your answer. b. Explain how to sort the entire list in O(n) time when f(n) is O( n). Justify your answer. c. How large can f(n) be for the entire list still to be sortable in O(n) time? Justify your answer.
13. Divide-and-conquer: [15 points] a. First write the in-place partition function that is used by the in-place quick sort algorithm. You may then call this function as a helper function in parts (b) and (c) below. b. Write a recursive divide-and-conquer algorithm for the selection problem that runs in O(n) average-case time and that uses O(lg n) average-case extra space beyond the input array. c. Write an algorithm for the selection problem that runs in O(n) average-case time and that uses O(1) extra space beyond the input array. Hint: each function call uses some space.