cse 780: design and analysis of algorithms
DESCRIPTION
CSE 780: Design and Analysis of Algorithms. Lecture 6: Quick sort Deterministic Randomized. Sorting Revisited !. Quick-sort Divide and conquer paradigm But in place sorting Worst case: Randomized quicksort: Expected running time: . A[m]: pivot. Divide and Conquer. - PowerPoint PPT PresentationTRANSCRIPT
CSE 2331/5331
CSE 2331/5331
Topic 6: Selection More on sorting
CSE 2331/5331
Order Statistics
Rank: The order of an element in the sorted sequence
Selection: Return the element with certain rank
CSE 2331/5331
Return rank(1) (i.e, minimum), or rank(n) (i.e, maximum) n-1 operations Best possible
Return both rank(1) and rank(n) Better than 2n-2? Yes
Return the median
Examples
CSE 2331/5331
Select ( r )
Return the element with rank r.
Straightforward approach: Sort, then return A[r] Takes time Compute a lot of extra, unnecessary information
CSE 2331/5331
Divide and Conquer Again!
Similar to QuickSort Select ( A, 1, n, r ) First, perform m = Partition(A, 1, n);
A[m]: pivot
Case 1: r = m
return A[m]
CSE 2331/5331
Select (A, 1, n, r )
A[m]: pivot
Case 2: r < m
return Select ( A, 1, m-1, r )
CSE 2331/5331
Select (A, 1, n, r )
A[m]: pivot
Case 3: r > m
return Select ( A, m+1, n, r-m )
CSE 2331/5331
Pseudo-code
Select ( A, s, t, r ) if ( s = t ) return A[s] ; m = Partition ( A, s, t ); if ( m = r+s-1) return A[m]; if ( m > r+s-1 ) return Select ( A, s, m-1, r ); else return Select ( A, m+1, t , r+s-m );
Select(A, 1, n, r)
T(n) = T( max(m-1, n-m) ) + O(n)
CSE 2331/5331
Analysis
Worst case
If lucky: always gets a balanced partition:
Lucky case:
CSE 2331/5331
Pseudo-code
Rand-Select ( A, s, t, r ) if ( s = t ) return A[s] ; m = Rand-Partition ( A, s, t ); if ( m = r+s-1) return A[m]; if ( m > r+s-1 ) return Rand-Select ( A, s, m-1, r ); else return Rand-Select ( A, m+1, t , r+s- m );
CSE 2331/5331
Rand-Select ( A, 1, n, r)
m = Rand-Partition ( A, 1, n ); if ( m = r) return A[m]; if ( m > r ) return Rand-Select ( A, 1, m-1, r ); else return Rand-Select ( A, m+1, n, r - m );
CSE 2331/5331
Solving Recursion
CSE 2331/5331
Recursion cont.
max(k-1, n-k) = k-1 if k > n/2n-k otherwise
Proof by substitution method that .
That is, the expected running time for randomized selection is
CSE 2331/5331
CSE 2331/5331
Remarks
One can in fact have a deterministic selection algorithm of time complexity Smart way to guarantee that one always find a good
partition
This induces a deterministic QuickSort algorithm with time complexity
CSE 2331/5331
Lower Bound for Sorting
Model What types of operations are allowed E.g: partial sum
Both addition and subtraction Addition-only model
For sorting: Comparison-based model
CSE 2331/5331
Decision Tree
> ai ajyes
> ak am
no
> as at
CSE 2331/5331
Example
For insertion sort with 3 elements
CSE 2331/5331
Decision Tree
Not necessary same height Worst case complexity:
Longest root-leaf path Each leaf:
A possible outcome I.e., a permutation of input
Every possible outcome should be some leaf #leaves ≥ n!
CSE 2331/5331
Lower Bound
Any binary tree of height h has at most leaves A binary tree with m leaves is of height at least lg m
Worst case complexity for any algorithm sorting n elements is : (lg (n!) ) = (n lg n) (by Stirling approximation)
CSE 2331/5331
Non-comparison Based Sorting
Assume inputs are integers [1, … k] k = O (n)
Count-Sort (A, n) (simplified version) Initialize array C[ 1,…k ] with C[ i ] = 0 for i = 1 to n do C[ A[ i ] ] ++ output based on C
Time and space complexity
Summary
Selection Linear time algorithm
Both by a randomized algorithm, and by a deterministic algorithm.
Sorting Under comparison model, requires comparisons MergeSort, QuickSort, optimal under this model.
CSE 2331/5331