Randomized%20Quick%20Sort%20Algorithm - PowerPoint PPT Presentation

About This Presentation
Title:

Randomized%20Quick%20Sort%20Algorithm

Description:

In traditional Quick Sort, we always pick the first element as the pivot for ... S = {S1, S2, S3, ..., Sn} If |S| 1 Then, Select an element X from S at random ... – PowerPoint PPT presentation

Number of Views:220
Avg rating:3.0/5.0
Slides: 12
Provided by: muha54
Learn more at: https://crystal.uta.edu
Category:

less

Transcript and Presenter's Notes

Title: Randomized%20Quick%20Sort%20Algorithm


1
Randomized Quick Sort Algorithm
  • Lecture Note 1
  • Prepared By Muhammed Miah

2
Randomized Quick Sort
  • In traditional Quick Sort, we always pick the
    first element as the pivot for partitioning.
  • The worst case runtime is O(n2) while the
    expected runtime is O(nlogn) over the set of all
    input.
  • Therefore, some input are born to have long
    runtime, e.g., an inversely sorted list.

3
Randomized Quick Sort
  • In randomized Quick Sort, we pick randomly an
    element as the pivot for partitioning.
  • The expected runtime of any input is O(nlogn).
  • Randomized Quick Sort is a probabilistic algorithm

4
Randomized Quick Sort (S)
  • Input a set of numbers S,
  • S S1, S2, S3, , Sn
  • If S gt 1 Then,
  • Select an element X from S at random
  • Partition S into 3 parts
  • Random Quick Sort (L)
  • Random Quick Sort (R)

L
X
R
5
Randomized Quick Sort
  • Example
  • S 2, 1, 3, 5, 4

1st pivot
2
1
2
3, 5, 4
2nd pivot
3rd pivot
5
1
sorted
3, 4
5
4
4th pivot
3
4
5th pivot
3
6
Randomized Quick Sort
  • Properties
  • Running time is unpredictable
  • It has randomization built into it
  • Nothing to do with input order
  • Worst unequal division when one side has zero
    number and other side has all numbers
  • Worst case running time n2

7
Analysis of Expected Running Time of Randomized
QS
  • Assume, S 1, 2, 3, 4, , n)
  • Let us consider a Boolean variable, Xij 1 i, j
    n, j gt i
  • Xij 1 if it is compared with j
  • Xij 0 otherwise
  • of variables we can create n2

X12 X13 Xn-1 n SUM
1 if X1 and X2 are compared, 0 otherwise SUM(X12, X13, , Xn-1 n)


AVG(X12) AVG(X13)
millions of trees are generated
AVG.
8
Analysis of Expected Running Time of Randomized
QS
  • Xij is a random variable
  • E? ? Xij ? ? EXij
  • linearity of expectation,
  • EX Y EX EY
  • EXij how often i and j compared

n-1
n
n-1
n
i1
jgti
i1
jgti
Horizontal definition
Vertical definition
9
Analysis of Expected Running Time of Randomized
QS
  • Level wise left to right (LR) order
  • consider set of numbers 1, 2, , 10
  • assume we got the tree
  • level wise order
  • 6 4 8 3 5 9 1 10 2
  • If Min(LRi, LRj gt Min(LR of i1, i2, ,
    j-1), then
  • i is NOT compared to j
  • otherwise it is compared

6
8
4
7
9
5
3
1
10
2
10
Analysis of Expected Running Time of Randomized
QS
  • P(Xij 1) 2/(j-i1)
  • EXij 2/(j-i1)
  • ? ? EXij ? ? 2/(j-i1)
  • ? ? 2/(j-i) ? ? 2/k let, j-i k
  • ? ? 2/k

n-1
n
n
n
i1
jgti
i1
jgti
n
n
n
n-i
i1
Ji
i1
k1
n
n
i1
k1
11
Analysis of Expected Running Time of Randomized
QS
  • 2? ? 1/k 2n(? 1/k)
  • ? 1/k 1/1 1/2 1/3 1/n
  • (Harmonic Series)
  • 2n(? 1/k) 2n logn O(nlogn)
  • So, Expected running time of randomized quick
    sort O(nlogn)

n
n
n
k1
i1
k1
n
k1
n
k1
Write a Comment
User Comments (0)
About PowerShow.com