# cse 780: design and analysis of algorithms

Post on 24-Feb-2016

42 views

Category:

## Documents

Embed Size (px)

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

PowerPoint Presentation - Topology Based Methods in Shape Representation

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

CSE 2331/5331Order StatisticsRank: The order of an element in the sorted sequence

Selection:Return the element with certain rankReturn rank(1) (i.e, minimum), or rank(n) (i.e, maximum)n-1 operationsBest possible

Return both rank(1) and rank(n) Better than 2n-2? Yes

Return the median

CSE 2331/5331ExamplesCSE 2331/5331Select ( r )CSE 2331/5331Divide and Conquer Again!Similar to QuickSortSelect ( A, 1, n, r )First, perform m = Partition(A, 1, n);

A[m]: pivotCase 1: r = mreturn A[m]CSE 2331/5331Select (A, 1, n, r )A[m]: pivotCase 2: r < mreturn Select ( A, 1, m-1, r )CSE 2331/5331Select (A, 1, n, r )A[m]: pivotCase 3: r > mreturn Select ( A, m+1, n, r-m )CSE 2331/5331Pseudo-codeSelect ( 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/5331AnalysisCSE 2331/5331Pseudo-codeRand-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/5331Rand-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/5331Solving RecursionCSE 2331/5331Recursion cont. max(k-1, n-k) = k-1if k > n/2n-k otherwise

CSE 2331/5331CSE 2331/5331RemarksCSE 2331/5331Lower Bound for SortingModelWhat types of operations are allowedE.g: partial sumBoth addition and subtractionAddition-only model

For sorting:Comparison-based model CSE 2331/5331Decision Tree> aiajyes> akamno> asatCSE 2331/5331ExampleFor insertion sort with 3 elementsCSE 2331/5331Decision TreeNot necessary same heightWorst case complexity:Longest root-leaf pathEach leaf: A possible outcomeI.e., a permutation of inputEvery possible outcome should be some leaf#leaves n!

CSE 2331/5331Lower BoundCSE 2331/5331Non-comparison Based SortingSummaryCSE 2331/5331