Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a...

47
Quicksort, Randomized Algorithms Lecture 4

Transcript of Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a...

Page 1: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

Quicksort, Randomized Algorithms

Lecture 4

Page 2: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.2

Quicksort

• Proposed by C.A.R. Hoare in 1962.

• Divide-and-conquer algorithm.

• Sorts “in place” (like insertion sort, but notlike merge sort).

• Very practical (with tuning).

Page 3: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.3

Divide and conquer

Quicksort an n-element array:

1. Divide: Partition the array into two subarraysaround a pivot x such that elements in lowersubarray x elements in upper subarray.

2. Conquer: Recursively sort the two subarrays.

3. Combine: Trivial.

x x x

Key: Linear-time partitioning subroutine.

Page 4: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.4

x

Running time = O(n) for n elements.

Partitioning subroutine

PARTITION(A, p, q) ⊳ A[ p . . q] x A[ p] ⊳ pivot = A[ p] i p for j p + 1 to q

do if A[ j] x then i i + 1

exchange A[i] A[ j] exchange A[ p] A[i] return i

x x ?

p i q j

Invariant:

Page 5: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.5

Example of partitioning

i j

6 10 13 5 8 3 2 11

Page 6: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.6

Example of partitioning

i j

6 10 13 5 8 3 2 11

Page 7: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.7

Example of partitioning

i j

6 10 13 5 8 3 2 11

Page 8: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.8

Example of partitioning

6 10 13 5 8 3 2 11

i j

6 5 13 10 8 3 2 11

Page 9: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.9

Example of partitioning

6 10 13 5 8 3 2 11

i j

6 5 13 10 8 3 2 11

Page 10: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.10

Example of partitioning

6 10 13 5 8 3 2 11

i j

6 5 13 10 8 3 2 11

Page 11: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.11

Example of partitioning

6 10 13 5 8 3 2 11

i j

6 5 3 10 8 13 2 11

6 5 13 10 8 3 2 11

Page 12: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.12

Example of partitioning

6 10 13 5 8 3 2 11

i j

6 5 3 10 8 13 2 11

6 5 13 10 8 3 2 11

Page 13: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.13

Example of partitioning

6 10 13 5 8 3 2 11

6 5 3 10 8 13 2 11

6 5 13 10 8 3 2 11

i j

6 5 3 2 8 13 10 11

Page 14: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.14

Example of partitioning

6 10 13 5 8 3 2 11

6 5 3 10 8 13 2 11

6 5 13 10 8 3 2 11

i j

6 5 3 2 8 13 10 11

Page 15: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.15

Example of partitioning

6 10 13 5 8 3 2 11

6 5 3 10 8 13 2 11

6 5 13 10 8 3 2 11

i j

6 5 3 2 8 13 10 11

Page 16: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.16

Example of partitioning

6 10 13 5 8 3 2 11

6 5 3 10 8 13 2 11

6 5 13 10 8 3 2 11

6 5 3 2 8 13 10 11

i

2 5 3 6 8 13 10 11

Page 17: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.17

Pseudocode for quicksort

QUICKSORT(A, p, r)

if p < r

then q PARTITION(A, p, r)

QUICKSORT(A, p, q)

QUICKSORT(A, q+1, r)

Initial call: QUICKSORT(A, 1, n)

Page 18: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.18

Analysis of quicksort

• Assume all input elements are distinct.

• In practice, there are better partitioningalgorithms for when duplicate inputelements may exist.

• Let T(n) = worst-case running time onan array of n elements.

Page 19: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.19

Worst-case of quicksort

• Input sorted or reverse sorted.

• Partition around min or max element.

• One side of partition always has no elements.

)(

)()1(

)()1()1(

)()1()0()(

2n

nnT

nnT

nnTTnT

(arithmetic series)

Page 20: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.20

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

Page 21: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.21

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

T(n)

Page 22: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.22

cn

T(0) T(n–1)

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

Page 23: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.23

cn

T(0) c(n–1)

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

T(0) T(n–2)

Page 24: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.24

cn

T(0) c(n–1)

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

T(0) c(n–2)

T(0)

(1)

Page 25: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.25

cn

T(0) c(n–1)

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

T(0) c(n–2)

T(0)

(1)

2

1

nkn

k

Page 26: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.26

cn

(1) c(n–1)

Worst-case recursion tree

T(n) = T(0) + T(n–1) + cn

(1) c(n–2)

(1)

(1)

2

1

nkn

k

T(n) = (n) + (n2)

= (n2)

h = n

Page 27: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.27

Best-case analysis (For intuition only!)

If we’re lucky, PARTITION splits the array evenly:

T(n) = 2T(n/2) + (n)

= (n lg n) (same as merge sort)

What if the split is always 10

9

10

1 : ?

)()(10

9

101 nnTnTnT

What is the solution to this recurrence?

Page 28: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.28

Analysis of “almost-best” case

)(nT

Page 29: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.29

Analysis of “almost-best” case

cn

nT101 nT

10

9

Page 30: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.30

Analysis of “almost-best” case

cn

cn101 cn

10

9

nT100

1 nT100

9 nT100

9 nT100

81

Page 31: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.31

Analysis of “almost-best” case

cn

cn101 cn

10

9

cn100

1 cn100

9 cn100

9 cn100

81

(1)

(1)

log10/9n

cn

cn

cn

…O(n) leaves

Page 32: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.32

log10

n

Analysis of “almost-best” case

cn

cn101 cn

10

9

cn100

1 cn100

9 cn100

9 cn100

81

(1)

(1)

log10/9n

cn

cn

cn

T(n) cn log10/9n + O(n)

cn log10n

O(n) leaves

(n lg n)

Lucky!

Page 33: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.33

More intuition

Suppose we alternate lucky, unlucky, lucky, unlucky, lucky, ….

L(n) = 2U(n/2) + (n) lucky

U(n) = L(n – 1) + (n) unlucky

Solving:

L(n) = 2(L(n/2 – 1) + (n/2)) + (n)

= 2L(n/2 – 1) + (n)

= (n lg n)

How can we make sure we are usually lucky?

Lucky!

Page 34: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.34

Randomized quicksort

IDEA: Partition around a random element.

• Running time is independent of the inputorder.

• No assumptions need to be made aboutthe input distribution.

• No specific input elicits the worst-casebehavior.

• The worst case is determined only by theoutput of a random-number generator.

Page 35: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.35

Randomized quicksort

analysis

Let T(n) = the random variable for the running time of randomized quicksort on an input of size n, assuming random numbers are independent.

For k = 0, 1, …, n–1, define the indicator random variable

Xk = 1 if PARTITION generates a k : n–k–1 split,

0 otherwise.

E[Xk] = Pr{Xk = 1} = 1/n, since all splits are equally likely, assuming elements are distinct.

Page 36: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.36

Analysis (continued)

T(n) =

T(0) + T(n–1) + (n) if 0 : n–1 split,

T(1) + T(n–2) + (n) if 1 : n–2 split, M

T(n–1) + T(0) + (n) if n–1 : 0 split,

1

0

)()1()(n

k

k nknTkTX .

Page 37: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.37

Calculating expectation

1

0

)()1()()]([n

kk nknTkTXEnTE

Take expectations of both sides.

Page 38: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.38

Calculating expectation

1

0

1

0

)()1()(

)()1()()]([

n

kk

n

kk

nknTkTXE

nknTkTXEnTE

Linearity of expectation.

Page 39: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.39

Calculating expectation

1

0

1

0

1

0

)()1()(

)()1()(

)()1()()]([

n

kk

n

kk

n

kk

nknTkTEXE

nknTkTXE

nknTkTXEnTE

Independence of Xk from other random choices.

Page 40: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.40

Calculating expectation

1

0

1

0

1

0

1

0

1

0

1

0

)(1)1(1)(1

)()1()(

)()1()(

)()1()()]([

n

k

n

k

n

k

n

kk

n

kk

n

kk

nn

knTEn

kTEn

nknTkTEXE

nknTkTXE

nknTkTXEnTE

Linearity of expectation; E[Xk] = 1/n .

Page 41: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.41

Calculating expectation

)()(2

)(1)1(1)(1

)()1()(

)()1()(

)()1()()]([

1

1

1

0

1

0

1

0

1

0

1

0

1

0

nkTEn

nn

knTEn

kTEn

nknTkTEXE

nknTkTXE

nknTkTXEnTE

n

k

n

k

n

k

n

k

n

kk

n

kk

n

kk

Summations have identical terms.

Page 42: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.42

Hairy recurrence

)()(2)]([1

2

nkTEn

nTEn

k

(The k = 0, 1 terms can be absorbed in the (n).)

Prove: E[T(n)] a n lg n for constant a > 0 .

Use fact: 21

2812

21 lglg nnnkk

n

k

(exercise).

• Choose a large enough so that a n lg ndominates E[T(n)] for sufficiently small n 2.

Page 43: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.43

Substitution method

)(lg2)(1

2

nkakn

nTEn

k

Substitute inductive hypothesis.

Page 44: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.44

Substitution method

)(81lg

212

)(lg2)(

22

1

2

nnnnn

a

nkakn

nTEn

k

Use fact.

Page 45: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.45

Substitution method

)(4

lg

)(81lg

212

)(lg2)(

22

1

2

nannan

nnnnn

a

nkakn

nTEn

k

Express as desired – residual.

Page 46: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.46

Substitution method

nan

nannan

nnnnn

a

nkakn

nTEn

k

lg

)(4

lg

)(81lg

212

)(lg2)(

22

1

2

if a is chosen large enough so that an/4 dominates the (n).

,

Page 47: Quicksort, Randomized Algorithmssourav/Lecture-04.pdf · Quicksort in practice •Quicksort is a great general-purpose sorting algorithm. •Quicksort is typically over twice as fast

L4.47

Quicksort in practice

• Quicksort is a great general-purposesorting algorithm.

• Quicksort is typically over twice as fastas merge sort.

• Quicksort can benefit substantially fromcode tuning.

• Quicksort behaves well even withcaching and virtual memory.