Preparation for Recitation and Class of Wed 21 March, 2018 ---------------------------------------------------------- 1a. PS5.Problem 3(a) : Constant-Fraction Splits in QuickSort. ******************** In best-case, we have a 50%- 50% split in every iteration : Thus the recurrence for the best case running time of quick-sort, if only key comparisons are counted is, T(n)=2T(n/2)+(n-1) or the simpler T(n) = 2T(n/2) +n i.e. T(n)=Theta(nlgn) In worst-case we have a 0%-100% split in every iteration : T(n)=T(n-1)+ (n-1) or the simpler T(n) = T(n-1) +n i.e. T(n)=Theta(n**2) PS5.Problem 3(a) What happens if we have a 33%-65% split in every iteration? 1.a.1 T(n) = T(n/3) + T(2n/3) +n PS5.Problem 3(a) What happens if we have a 10%-90% split in every iteration? 1.a.2 T(n) = T(n/10) + T(9n/10) +n PS5.Problem 3(a) What happens if we have a 50%-50% split in every iteration? 1.a.3 T(n) = T(n/2) + T(n/2) +n 1b. PS5.Problem 5 : T(n) = T(an)+T(bn)+n , a+b <1 ****************** 1b. Case 1a. above reminds of PS5.Problem 5 where for a+b <1 or an+bn < n T(n) = T(an)+T(bn)+n gives a solution T(n) = O(n) (in fact Theta(n)). If we generalize Cases 1.a.[1-3] we have T(n) = T(an) + T(bn) +n where a+b =1 i.e. b=1-a and T(n) = T(an) + T((1-a)n) +n Thus in all of Cases 1a.[1-3] we have (1/3+2/3 =1 ), (1/10+9/10=1), (1/2 +1/2 =1). Therefore PS5.Problem 5 cannot be used and to answer the question, 1c. Question: Solve T(n) = T(an)+T(bn)+n , a+b =1 i.e T(n) = T(an) + T((1-a)n) +n ******************* Answer: (1.a.1) In Textbook (using recursion tree/substitution method). See Figure 4.6 and pages 91-92. ===> T(n) = Theta (nlgn) (1.a.2) Solved in PS5.Problem(3a) using recursion tree method. ===> T(n) = Theta (nlgn) (1.a.3) Solved in class with all three methods ===> T(n) = Theta (nlgn) (1c) Solve the general problem using substitution method. guess T(n) = cnlgn as an answer and do not forget PS1.Problem 7, After some algebraic manipulations, determine that c= -1 / (alg(a)+(1-a)lg(1-a)) The denominator of c, by PS1.Problem 7 is minimized for a=1/2 it is -1 and thus c = +1. For other values of a, the denominator is a negative number. In all cases ===> T(n) = Theta (nlgn) But the constant c of Theta(nlgn) depends on a as shown above. The conclusion is "The more unbalanced the split is the larger the constant of Theta(nlgn)" 1d. Does QuickSort sort in-place ? NO ************ Recursion in quicksort requires storing the (l,r) values for the recursive calls. Thus a stack is needed. In the worst-case the stack can be linear in size, if you are careful [Problem 7-4 of the textbook, page 188] only Theta(lgn) stack space can be used. This is the reason QuickSort does not sort IN-PLACE. In MergeSort the problem is not the (l,r) values. They can be computed deterministically such as (1,n),and for example (1,n/2), (n/2+1,n) and so on. The 'does not sort IN-PLACE' of MergeSort is because of the merging step. 1e. Is QuickSort stable ? NO . See PS5.Problem 3(b) ************* 1f. Worst-case running time of quicksort (math-based not recursion-tree based). ************ Subject 5 . [pages 7-8] and HW1.last problem!!!! 1g. Randomized QuickSort ************ Subject 5. [page 9] 1h. Analysis of Randomized QuickSort. Worst-case r.t. ; Average-case r.t. ************* 1.i Binary Search vs Linear Search . ************** Upper bound of running time Read PS2.Problem 5 1.j Decision Trees ****************** Read Subject 6, pages 3-5. 1.k Decision Tree for Binary Search ****************** A[1] A[2] A[3] k== A[2] / || \ Height of Tree is 2 / out(2) \ Worst case # of comparisons is 2 / \ A comparison is a k== A[ ? ] operation k==A[1] k==A[3] / || \ / || \ / out(1) \ / out(3) \ / \ / \ -1 -1 -1 -1 At level 0 we have 2**0 out statement At level 1 we have 2**1 out statement ... At level h-1 we have 2**(h-1) out statements Height of tree == Worst case number of comparisons IF we have array A[1] A[2]... A[n] we must have at least n out statements 2**0 + 2**1 + ... + 2** h-1 >= n 2**h -1 >= n 2**h >= n+1 h >= ceil(lg(n+1)) Height of tree is h == Worst case # of comparisons >= ceil(lg(n+1)) 1.l Lower bound and Upper bound for binary search ********************** 1.k shows that there can be no search of a sorted array performing fewer than ceil(lg(n+1)) comparisons in the worst case 1.i shows that binary search performs no more than ceil(lg(n+1)) comparisons in the worst case. Both of them show BinarySearch as shown is OPTIMAL for a sorted array. The ceil(lg(n+1)) is optimal for the worst-case Note: We are talking about BEST and WORST worst-case running time bound. Note: Naturally, if we are lucky, if we are done with binary search in 1 comparison! 1.m Decision Tree based sorting ************** PS2.Problem 7 (sort 4 keys with 5 comparisons in the worst case) PS5.Problem 7 (sort 5 keys with 7 comparisons in the worst case) 1.n. Selection [ Week 4] ************** Read Subject 6, page 13 1.o Selection: Min , Maximum [Week 2-3] ************** Read Subject 1, pages 23-24 including lower-bound 1.p Selection: Min and 2nd-min [Similar to Midterm Problem 7] **************** Find Min and 2nd-Min in fewer than 2n-2 comparisons Find Min and 2nd-Min in fewer than 2n-3 comparisons Find Min and 2nd-Min in about n+lgn-2 comparisons [Subject 6, page 14] 1.q Selection: Min + Max ********** Min, Max in 2n-2 or 2n-3 comparisons is trivial. 1.r Selection: Min + Max using Divide+Conquer ; assume n is power of 2 Show an 3n/2-2 comparison count. ********** PS4. Problem (7b) 1.s Selection: Min + Max using No divide + conquer ********** No divide + conquer : pair keys [Subject 6, page 14] Assume n is even; deal with odd case separately N= n/2 A[1]:A[2], A[3]:A[4] , ... , A[n-1]:A[n] N= n/2 comparisons m1 M1 m2 M2 mN MN min = minimum of m1, m2, mN using N-1 comparisons max = maximum of M1, M2, MN using N-1 comparisons Total comparisons count is N+(N-1)+(N-1)=n/2+n/2-1+n/2-1 = 3n/2 -2.