450 likes | 467 Views
Learn about an efficient sorting algorithm to find elements in an array with analysis and proofs. Includes practical examples and problem solutions.
E N D
CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu www.cs.rochester.edu/~stefanko/Teaching/06CS282
Quiz – problem 2 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ” r=2 m=1 B=5 4 5 l=1
Quiz – problem 2 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ”
Quiz – problem 2 B A B A[l..r] 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ”
Quiz – problem 2 B A B A[l..r] 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ” A[m]<B (B A B A[m+1..r])
Quiz – problem 2 B A B A[l..r] 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ” DONE
Quiz – problem 2 B A B A[l..r] 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ” DONE ?
Quiz – problem 2 B A B A[l..r] 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ” Still need to prove that the algorithm terminates.
Quiz – problem 2 B A B A[l..r] 1 l 1, r n 2 while l < r do 3 m (l+r)/2 4 if A[m] < B then 5 l m+1 6 else 7 r m 8 fi 9 od ANSWER = “ is A[l]=B ” Q = r-l Q’ = r-((l+r)/2 +1) Q’ = (l+r)/2 -l
Quiz – problem 3 PROBLEM: rotate an array in-place this type problem given on interviews in MSFT, GOOG homework
Recurrences T(n) T( n/2 ) + T( n/2 ) + c.n We “showed” : T(n)=O(n log n) for T(n) 2 T( n/2 ) + c.n
Recurrences T(n) 2 T( n/2 ) + c.n T(1) = O(1) Proposition: T(n) d.n.lg n Proof: T(n) 2 T( n/2 ) + c.n 2 d (n/2).lg (n/2) + c.n = d.n.( lg n – 1 ) + cn = d.n.lg n + (c-d).n d.n.lg n
Recurrences T(n) T( n/2 ) + T( n/2 ) + c.n T(n) 2 T( n/2 ) + c.n G(n) = T(n+2) G(n) = T(n+2) T(n/2+1) + T(n/2+1) + c.n = G(n/2-1) + G(n/2-1) + c.n
Recurrences • useful • guess – substitute (prove) • or use “Master theorem” • T(n) = a T(n/b) + f(n) • If f(n) = O(nc-) then T(n) =(nc) • If f(n) = (nc) then T(n) =(nc.log n) • If f(n) = (nc+) then T(n)=(f(n)) • if a.f(n/b) d.f(n) for some d<1 and n>n0 c=logb a
Recurrences • T(n) = a T(n/b) + f(n) • If f(n) = O(nc-) then T(n) =(nc) • If f(n) = (nc) then T(n) =(nc.log n) • If f(n) = (nc+) then T(n)=(f(n)) • if a.f(n/b) d.f(n) for some d<1 and n>n0 c=logb a T(n) = 3 T(n/2) + (n) T(n) = 2T(n/2) + (n.log n)
Finding the minimum min A[1] for i from 2 to n do if A[i]<min then min A[i] How many comparisons?
Finding the minimum How many comparisons? comparison based algorithm: The only allowed operation is comparing the elements
Finding the k-th smallest element k = n/2 = MEDIAN
8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 6 2 8 3 6 9 1 1 1 7 5 2 8 8 3 Finding the k-th smallest element
Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 6 2 8 3 6 9 1 1 1 7 5 2 8 8 3 1) sort each 5-tuple
Finding the k-th smallest element 6 2 8 3 6 9 1 1 1 7 5 2 8 8 3 1) sort each 5-tuple
Finding the k-th smallest element 1 2 8 3 6 9 6 1 1 7 5 2 8 8 3 1) sort each 5-tuple
Finding the k-th smallest element 1 2 8 3 6 9 6 1 1 7 5 2 8 8 3 1) sort each 5-tuple
Finding the k-th smallest element 1 1 8 3 2 9 6 5 1 7 6 2 8 8 3 1) sort each 5-tuple
Finding the k-th smallest element TIME = ? 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9 1) sort each 5-tuple
1 1 1 3 2 2 6 5 3 7 6 7 8 8 9 Finding the k-th smallest element TIME = (n) 1) sort each 5-tuple
Finding the k-th smallest element 2) find median of the middle n/5 elements TIME = ? 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9
Finding the k-th smallest element 2) find median of the middle n/5 elements TIME = T(n/5) 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9
Finding the k-th smallest element At least ? Many elements in the array are X 1 1 1 3 2 2 6 5 3 7 6 7 8 8 9
Finding the k-th smallest element At least ? Many elements in the array are X 1 1 1 2 2 3 3 5 6 7 6 7 9 8 8
Finding the k-th smallest element At least 3n/10 elements in the array are X 1 1 1 2 2 3 3 5 6 7 6 7 9 8 8
Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 1 1 1 2 2 3 3 5 6 7 6 7 9 8 8 At least 3n/10 elements in the array are X
Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 >X X 3 2 1 5 8 8 9 7 8 3 1 2 1 6 6 At least 3n/10 elements in the array are X
Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 >X X 3 2 1 5 8 8 9 7 8 3 1 2 1 6 6 Recurse, time ? At least 3n/10 elements in the array are X
Finding the k-th smallest element 8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 >X X 3 2 1 5 8 8 9 7 8 3 1 2 1 6 6 Recurse, time T(7n/10) At least 3n/10 elements in the array are X
8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 1 1 6 1 1 2 8 1 1 3 3 3 6 2 2 2 2 9 6 1 6 1 5 5 3 3 1 7 7 7 5 6 6 7 7 2 8 8 8 8 8 8 9 9 3 Finding the k-th smallest element 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 X >X 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse
8 2 6 5 8 8 9 2 3 6 3 1 7 1 1 1 6 1 1 1 2 8 1 1 3 3 3 2 2 6 2 2 9 1 6 6 5 5 1 3 1 3 7 7 7 6 5 6 7 2 7 8 8 8 8 8 8 9 3 9 Finding the k-th smallest element 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 (n) X >X (n) 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse T(7n/10) T(n/5)
Finding the k-th smallest element T(n) T(n/5) + T(7n/10) + O(n)
Finding the k-th smallest element T(n) T(n/5) + T(7n/10) + O(n) T(n) d.n Induction step: T(n) T(n/5) + T(7n/10) + O(n) d.(n/5) + d.(7n/10) + O(n) d.n + (O(n) – dn/10) d.n
Why 5-tuples? 3 1 7 6 1 5 9 1 2 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 3 6 9 1 1 1 (n) 7 5 2 X >X (n) 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse 1 1 1 3 5 2 7 6 9 1 1 1 3 5 2 7 6 9
Why 5-tuples? 3 1 7 6 1 5 9 1 2 1 2 5 8 2 6 3 7 8 6 1 8 9 1 3 3 6 9 1 1 1 (n) 7 5 2 X >X (n) 1 2 1 8 7 3 2 3 1 5 8 9 6 8 6 recurse 1 1 1 3 5 2 7 6 9 T(2n/3) T(n/3) 1 1 1 3 5 2 7 6 9
Why 5-tuples? T(n) = T(n/3) + T(2n/3) + (n)
Why 5-tuples? T(n) = T(n/3) + T(2n/3) + (n) T(n) c.n.ln n Induction step: T(n) = T(n/3) + T(2n/3) + (n) c.(n/3).ln (n/3) + c.(2n/3).ln (2n/3) + (n) c.n.ln n - c.n.((1/3)ln 3+(2/3)ln 3/2)+(n) c.n.ln n