Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as...

31
Chapter 7 Quicksort Ack: This presentation is based on the lecture sli des from Hsu, Lih-Hsing, as well as various materia ls from the web.

Transcript of Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as...

Page 1: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

Chapter 7

QuicksortAck: This presentation is based on the lecture slides from Hsu, Lih-Hsi

ng, as well as various materials from the web.

Page 2: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 2

7.1 Description of quicksort

Quicksort an n-element array:• Divide: Partition the array into two subarrays aro

und a pivot x such that elements in lower subarray ≤ x ≤ elements in upper subarray.

≤ x x ≥ x

• 2.Conquer: Recursively sort the two subarrays.• 3. Combine: Trivial.

Key: Linear-time partitioning subroutine.

Page 3: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 3

QUICKSORT(A,p,r)

1 if p r

2 then q PARTITION A p r ( , , )

3 QUICKSORT(A,p,q-1)

4 QUICKSORT(A,q+1,r)

Initial call: QUICKSORT(A, 1, n)

Page 4: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 4

Partition(A, p, r)1 x A[r]2 i p – 13 for j p to r -1 4 do if A[j] ≤ x5 then i i + 16 exchange A[i] A[j]7 exchange A[i+1] A[r] 8 return i+1

Page 5: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 5

Loop Invariant

1. All entries in A[p..i] are ≤ pivot.

2. All entries in A[i+1..j-1] are > pivot.

3. A[r] = pivot.

Page 6: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 6

The operation of Partition on a sample array1 x A[r]2 i p – 13 for j p to r -1 4 do if A[j] ≤ x5 then i i + 16 exchange A[i] A[j]7 exchange A[i+1] A[r] 8 return i+1

Page 7: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 7

Two cases for one iteration of procedure Partition

Complexity: Partition on A[p…r] is (n) where n = r –p +1

1 x A[r]2 i p – 13 for j p to r -1 4 do if A[j] ≤ x5 then i i + 16 exchange A[i] A[j]7 exchange A[i+1] A[r] 8 return i+1

Why???(Exercise 7.1-3)

Page 8: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 8

Another Partitioning subroutine

Invariant: x ≤x ≥x ?

p i j r

Partition(A, p, r) 1 x A[p]2 i p3 for j p+1 to r 4 do if A[j] ≤ x5 then i i + 16 exchange A[i] A[j]7 exchange A[i] A[p] 8 return i

Select first element as pivot

Page 9: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 9

Another partitioning example

6 10 13 5 8 3 2 11

i j

Page 10: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 10

Page 11: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 11

Page 12: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 12

Page 13: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 13

Page 14: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 14

Page 15: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 15

Page 16: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 16

Page 17: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 17

Page 18: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 18

Page 19: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 19

Page 20: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 20

Page 21: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 21

7.2 Performance of quicksort

Worst-case partition:

)(

)()1(

)()0()1()(

2n

nnT

nTnTnT

Best-case partition:

)lg()(

)()2/(2)(

nnnT

nnTnT

Why???(Exercise 7.2-1)

Why???

The input array is already completely sorted.No better than insertion sort.

Page 22: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 22

• Quicksort’s average running time is much closer to the best case than to the worst case.

• Imagine that PARTITION always produces 9-to-1 split. • Any split of constant proportionality will yield a recur

sion tree of depth , where the cost at each level is O(n).

Balanced partition: )lg()( nnnT

)lg()(

)()10/()10/9()(

nnnT

nnTnTnT

)(lg n

Page 23: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 23

Page 24: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 24

Intuition for the average case T(n) = (n lg n)

• Splits will not always be constant.• Mix of good and bad splits throughout

the recursion tree

Page 25: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 25

7.3 Randomized quicksort• We have assumed that

all input permutations are equally likely. (not always true)

• To correct this, add randomization to randomly permute the input array

• Instead, we use random samplingpicking one element at random.

• Don’t always use A[r] as the pivot, and randomly pick an element from the subarray that is being sorted.

Page 26: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 26

RANDOMIZED_PARTITION(A, p, r)

1 ),( rpRANDOMi

2 exchange ][][ iArA

3 return PARTITION(A, p, r)

RANDOMIZED_QUICKSORT(A, p, r)

1 if p r

2 then ),,(_ rpAPARTITIONRANDOMIZEDq

3 RANDOMIZED_QUICKSORT(A, p, q-1)

4 RANDOMIZED_QUICKSORT(A, q+1, r)

Page 27: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 27

7.4 Analysis of quicksort7.4.1 Worst-case analysis

)())1()(()( max10

nqnTqTnTnq

guess T n cn( ) 2

2

2

22

10

22

10

)()1(2

)())1((

)())1(()(

max

max

cn

nnccn

nqnqc

nqnccqnT

nq

nq

pick the constant c large enough so that the 2 1c n( ) term dominates

the ( )n term.

T n n( ) ( ) 2

Page 28: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 28

7.4.2 Expected running time

• Running time and comparisons• Lemma 7.1

• Let X be the number of comparisons performed in line 4 of partition over the entire execution of Quicksort on an n-element array. Then the running rime of Quicksort is O(n+X)

Page 29: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 29

we define

{zi is compared to zj},

{zi is compared to zj}

IX ij

.1

1 1

n

i

n

ijijXX

1

1 1

1

1 1

1

1 1

Pr

][

n

i

n

ij

n

i

n

ijij

n

i

n

ijij

XE

XEXE

Page 30: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 30

Pr{zi is compared to zj} = Pr{zi or zj is first pivot chosen from Zij}

= Pr{zi is first pivot chosen from Zij}

+ Pr{zj is first pivot chosen from Zij}

1

1

1

1

ijij 1

2

ij

1

1 1

.1

2][

n

i

n

ij ijXE

Page 31: Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.

20071026 chap07 Hsiu-Hui Lee 31

)lg(

)(lg

2

1

2

1

2][

1

1

1

1 1

1

1 1

1

1 1

nnO

nO

k

k

ijXE

n

i

n

i

n

k

n

i

in

k

n

i

n

ij

by pp. 1060 A.7