cse 780: design and analysis of algorithms

22
CSE 2331/5331 CSE 2331/5331 Topic 6: Selection More on sorting

Upload: nevin

Post on 24-Feb-2016

60 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

CSE 2331/5331

Topic 6: Selection More on sorting

Page 2: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Order Statistics

Rank: The order of an element in the sorted sequence

Selection: Return the element with certain rank

Page 3: CSE 780: Design and Analysis of Algorithms

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

Page 4: CSE 780: Design and Analysis of Algorithms

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

Page 5: CSE 780: Design and Analysis of Algorithms

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]

Page 6: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Select (A, 1, n, r )

A[m]: pivot

Case 2: r < m

return Select ( A, 1, m-1, r )

Page 7: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Select (A, 1, n, r )

A[m]: pivot

Case 3: r > m

return Select ( A, m+1, n, r-m )

Page 8: CSE 780: Design and Analysis of Algorithms

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)

Page 9: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Analysis

Worst case

If lucky: always gets a balanced partition:

Lucky case:

Page 10: CSE 780: Design and Analysis of Algorithms

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 );

Page 11: CSE 780: Design and Analysis of Algorithms

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 );

Page 12: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Solving Recursion

Page 13: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Recursion cont.

max(k-1, n-k) = k-1 if k > n/2n-k otherwise

Proof by substitution method that .

Page 14: CSE 780: Design and Analysis of Algorithms

That is, the expected running time for randomized selection is

CSE 2331/5331

Page 15: CSE 780: Design and Analysis of Algorithms

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

Page 16: CSE 780: Design and Analysis of Algorithms

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

Page 17: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Decision Tree

> ai ajyes

> ak am

no

> as at

Page 18: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Example

For insertion sort with 3 elements

Page 19: CSE 780: Design and Analysis of Algorithms

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!

Page 20: CSE 780: Design and Analysis of Algorithms

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)

Page 21: CSE 780: Design and Analysis of Algorithms

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

Page 22: CSE 780: Design and Analysis of Algorithms

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