{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04 - Randomized Partition &amp; Randomize Selection

# 04 - Randomized Partition &amp; Randomize Selection -...

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

Part I: Divide and Conquer Lecture 4: Randomized Partition and Randomized Selection Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer

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

View Full Document
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 divide-and-conquer algorithm Analysis of the divide-and-conquer 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

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

View Full Document
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

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

View Full Document
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 = p-1; for j = p to r-1 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

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

View Full Document
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 .. r ] Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer
Outline Outline: Partition Randomized partition and randomized quicksort Analysis of the randomized quicksort The selection problem First solution: Selection by sorting A divide-and-conquer algorithm Analysis of the divide-and-conquer algorithm Lecture 4: Randomized Partition and Randomized Selection Part I: Divide and Conquer

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern