module03

# module03 - Module 3 Sorting and Randomized Algorithms CS...

This preview shows pages 1–8. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module 3: Sorting and Randomized Algorithms CS 240 - Data Structures and Data Management Arne Storjohann Based on lecture notes by R. Dorrigiv and D. Roche School of Computer Science, University of Waterloo Fall 2010 Arne Storjohann (CS, UW) CS240 - Module 3 Fall 2010 1 / 24 Selection vs. Sorting We have already seen some algorithms for the selection problem : Given an array A of n numbers, find the k th largest number. (note: we always count from zero, so 0 ≤ k < n ) Best heap-based algorithm had time cost Θ( n + k log n ). For median selection, k = n 2 , giving cost Θ( n log n ). This is the same cost as our best sorting algorithms. Question : Can we do selection in linear time? Arne Storjohann (CS, UW) CS240 - Module 3 Fall 2010 2 / 24 Selection vs. Sorting We have already seen some algorithms for the selection problem : Given an array A of n numbers, find the k th largest number. (note: we always count from zero, so 0 ≤ k < n ) Best heap-based algorithm had time cost Θ( n + k log n ). For median selection, k = n 2 , giving cost Θ( n log n ). This is the same cost as our best sorting algorithms. Question : Can we do selection in linear time? The quick-select algorithm answers this question in the affirmative. Arne Storjohann (CS, UW) CS240 - Module 3 Fall 2010 2 / 24 Selection vs. Sorting We have already seen some algorithms for the selection problem : Given an array A of n numbers, find the k th largest number. (note: we always count from zero, so 0 ≤ k < n ) Best heap-based algorithm had time cost Θ( n + k log n ). For median selection, k = n 2 , giving cost Θ( n log n ). This is the same cost as our best sorting algorithms. Question : Can we do selection in linear time? The quick-select algorithm answers this question in the affirmative. Observation : Finding the element at a given position is tough, but finding the position of a given element is simple. Arne Storjohann (CS, UW) CS240 - Module 3 Fall 2010 2 / 24 Crucial Subroutines quick-select and the related algorithm quick-sort rely on two subroutines: choose-pivot ( A ): Choose an index i such that A [ i ] will make a good pivot (hopefully near the middle of the order). partition ( A , p ): Using pivot A [ p ], rearrange A so that all items ≤ the pivot come first, followed by the pivot, followed by all items greater than the pivot. Arne Storjohann (CS, UW) CS240 - Module 3 Fall 2010 3 / 24 Selecting a pivot Ideally, we would select a median as the pivot. But this is the problem we’re trying to solve! First idea : Always select first element in array choose-pivot1 ( A ) 1. return We will consider more sophisticated ideas later on. Arne Storjohann (CS, UW) CS240 - Module 3 Fall 2010 4 / 24 Partition Algorithm partition ( A , p ) A : array of size n , p : integer s.t. 0 ≤ p < n 1. swap ( A [0] , A [ p ]) 2. i ← 1 , j ← n- 1 3. while i < j do 4. while A [ i ] ≤ A [0] and i < n do 5. i ← i + 1 6. while A [ j ] > A [0] and j > do 7. j ← j- 1 8. if i < j then 9. swap...
View Full Document

## This note was uploaded on 01/14/2012 for the course CS 246 taught by Professor Wormer during the Winter '08 term at Waterloo.

### Page1 / 39

module03 - Module 3 Sorting and Randomized Algorithms CS...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online