This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Part I: Divide and Conquer Lecture 4: Randomized Partition and Randomized Selection Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer Objective and Outline Objective : Show two examples that use both (1) divide and conquer and (2) randomization Reference : Chapter 7, 9 of CLRS Outline: Partition Basic partition Randomized partition and randomized quicksort Analysis of the randomized quicksort Selection The selection problem First solution: Selection by sorting A divideandconquer algorithm Analysis of the divideandconquer algorithm Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer Partition Given : An array of distinct numbers Partition : Rearrange the array A [ p .. r ] into two (possibly empty) subarrays A [ p .. q 1] and A [ q + 1 .. r ] such that A [ u ] ≤ A [ q ] < A [ v ] , for any p ≤ u ≤ q 1 and q + 1 ≤ v ≤ r x p r q x x x = A[r] Quicksort works by: 1 calling partition first 2 recursively sorting A [ p .. q 1] and A [ q + 1 .. r ] Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer The Idea of Partition( A , p , r ) Remark : In COMP 171, you learned one way to do partition Now, we discussed another approach. Use A [ r ] as the pivot , and grow partition from left to right i p j r x x x unrestricted 1 Initially ( i , j ) = ( p 1 , p ) 2 Increase j by 1 each time to find a place for A [ j ] At the same time increase i when necessary 3 Stops when j = r Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer One Iteration of the Procedure Partition Increase j by 1 each time to find a place for A [ j ] At the same time increase i when necessary i p j r x x x >x j p i x x x r r p x x i j x < x p i j r x x (A) A[j] > x (B) A[j] < x 1 Only increase j by 1 2 i ← i + 1. A [ i ] ↔ A [ j ]. j ← j + 1 Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer Example: The Operation of Partition( A , p , r ) i j j j p i j p i j p i j p i j, r p i j, r r r r r r r r p, j p, i p, i p, i 2 8 7 4 1 3 5 6 2 2 2 2 2 2 2 2 1 1 1 1 1 3 3 3 3 8 8 8 8 8 8 8 7 7 7 7 7 7 5 5 5 6 6 8 7 1 3 5 6 4 7 1 3 5 6 4 1 3 5 6 4 3 5 6 4 5 6 4 6 4 4 4 (1) (2) (3) (4) (5) (6) (9) (7) (8) Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer The Partition( A , p , r ) Algorithm Partition(A, p, r) begin x = A[r]; // A[r] is the pivot element i = p1; for j = p to r1 do if A[j] ≤ x then i = i+1; exchange A[i] and A[j]; end end exchange A[i+1] and A[r]; // put pivot in position return i+1 // q = i+1 end Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer Running Time of Partition( A , p , r ) Partition( A , p , r ): x = A [ r ] i = p 1 for j = p to r 1 if A [ j ] ≤ x ( r p ) i = i + 1 exchange A [ i ] ↔ A [ j ] exchange A [ i + 1] ↔ A [ r ] return i + 1 Total: ( r p ) Running time is Θ( r p ) linear in the length of the array A [ p ......
View
Full
Document
This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.
 Spring '07
 ARYA
 Algorithms

Click to edit the document details