Tue 6 Mar 2018 @ 16:00 ---------------------- 1. Wed 7 Mar , 2018 : Recitation 1a. 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 25%-75% split in every iteration? 1.a.1 T(n) = T(n/4) + T(3n/4) +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 30%-70% split in every iteration? 1.a.3 T(n) = T(3n/10) + T(7n/10) +n 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/4+3/4 =1 ), (1/10+9/10=1), (3/10+7/10=1). Therefore PS5.Problem 5 cannot be used and to answer the question, 1c. What is the solution for the various Case 1.a.[1-3] when a+b=1? i.e. the solution of T(n) = T(an) + T((1-a)n) +n Answer: (Method 1) T(n) = Theta (nlgn) or read PS5.Problem 3(a) (Method 2) If you use the substitution method, guess T(n) = cnlgn - n as an answer and do not forget PS1.Problem 7, you get the same answer. The conclusion is "The more unbalanced the split is the larger the constant(s) of Theta(nlgn)" ('constants mean' c_1 and c_2, c_1 nlgn <= T(n) <= c_2 nlgn ) ('constant c_2 is the more important one if we drop the plural and say') "The more unbalanced the split is the larger the constant of Theta(nlgn) is" 1d. Is QuickSort 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. 2. Wed 7 Mar, 2018 : Class substitution (recitation after the break) plus the following review. 2.a Binary Search vs Linear Search . Upper bound of running time Read PS2.Problem 5 2.b Decision Trees Read Subject 6, pages 3-5. 2.c 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) 2.d Selection [ Week 4] Read Subject 6, page 13 2.e Selection: Min , Maximum [Week 2-3] Read Subject 1, pages 23-24 including lower-bound 2.f 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] 2.g Selection: Min + Max Min, Max in 2n-2 or 2n-3 comparisons is trivial. 2.g.1 Divide+Conquer; assume n is power of 2 Show an 3n/2-2 comparison count. 2.g.2 No divide + conquer : pair keys [Subject 6, page 14]