23
1 QuickSort • Worst time: (n 2 ) • Expected time: (nlgn) – Constants in the expected time are small • Sorts in place

1 QuickSort Worst time: (n 2 ) Expected time: (nlgn) – Constants in the expected time are small Sorts in place

Embed Size (px)

Citation preview

Page 1: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

1

QuickSort

• Worst time: (n2)• Expected time: (nlgn)

– Constants in the expected time are small

• Sorts in place

Page 2: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

2

QuickSort (cont)

• DIVIDE – Partition A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is A[q] each element of A[q+1..r]

• Conquer – Sort the two subarrays by recursive calls to Quicksort

• Combine – Since subarrays are sorted in place, they are already sorted

Page 3: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

3

QuickSort (cont)

To sort entire array:QuickSort( A, 1, length(A) )

QuickSort( A, p, r )

1. if p < r

2. q Partition( A, p, r )

3. QuickSort( A, p, q-1 )

4. QuickSort( A, q+1, r )

Page 4: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

4

QuickSort (cont)

Partition( A, p, r )x A[ r ]i p – 1for j p to r-1

if A[ j ] xi i + 1Exchange( A[ i ], A[ j ] )

Exchange( A[ i+1 ], A[ r ] )return i+1

Page 5: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

5

QuickSort (cont)

1 2 3 4 5 6 7 8

19 9 62 74 94 13 90 48p,i j r,x

1 2 3 4 5 6 7 8

19 9 62 74 94 13 90 48i p,j r,x

1 2 3 4 5 6 7 8

19 9 62 74 94 13 90 48p i j r,x

1 2 3 4 5 6 7 8

19 9 62 74 94 13 90 48p i j r,x

Page 6: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

6

QuickSort (cont)1 2 3 4 5 6 7 8

19 9 62 74 94 13 90 48p i j r,x

1 2 3 4 5 6 7 8

19 9 62 74 94 13 90 48p i j r,x

1 2 3 4 5 6 7 8

19 9 13 74 94 62 90 48p i j r,x

1 2 3 4 5 6 7 8

19 9 13 48 94 62 90 74p i r

Return i+1which is 4

Page 7: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

7

Performance of QuickSort

• Partition function’s running time - (n)• Running time of QuickSort depends on the

balance of the partitions– If balanced, QuickSort is asymptotically as fast as

MergeSort– If unbalanced, it is asymptotically as bad as

Insertion Sort

Page 8: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

8

Performance of QuickSort (cont)

• Worst-case Partitioning– Partitions always of size n-1 and 0– Occurs when array is already sorted– Recurrence for the running time:

)(

)()1(

)()0()1()(

2n

nnT

nTnTnT

Page 9: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

9

Performance of QuickSort (cont)cn

c(n-1)

c(n-2)

...

n

cn

c(n-1)

c(n-2)

c

Total: (n2)

)(2

)1( 2

11

nnn

iccin

i

n

i

Page 10: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

10

Performance of QuickSort (cont)

• Best-case Partitioning– Partitions always of size n/2 and n/2-1– The recurrence for the running time:

)lg()(

)()(2)( 2

nnnT

nTnT n

Case 2 of Master Method

Page 11: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

11

Performance of QuickSort (cont)

• Balanced Partitioning– Average-case is closer to best-case– Any split of constant proportionality (say 99 to

1) will have a running time of (nlgn)• The recurrence will be

• Because it yields a recursion tree of depth (lgn), where cost at each level is (n)

• See page 151 (new book) for picture– Or next slide

cnnTnTnT )()()( 1001

10099

Page 12: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

12

Performance of QuickSort (cont)

c(n/100) c(99n/100)

cn

c(n/10000) c(99n/10000) c(99n/10000) c(9801n/10000)

T(1)

T(1)

T(1)T(1)

Total: (nlgn)

log100ncn

cn

cn

cn

cnlog100/99n

Page 13: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

13

Performance of QuickSort (cont)

• Intuition for the average case– The behavior depends on the relative ordering of

the values• Not the values themselves

– We will assume (for now) that all permutations are equally likely

– Some splits will be balanced, and some will be unbalanced

Page 14: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

14

Performance of QuickSort (cont)

– In a recursion tree for an average-case, the “good” and “bad” splits are distributed randomly throughout the tree

– For our example, suppose• Bad splits and good splits alternate• Good splits are best-case splits• Bad splits are worst-case splits• Boundary case (subarray size 0) has cost of 1

Page 15: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

15

Performance of QuickSort (cont)

– The (n-1) cost of the bad split can be absorbed into the (n) cost of the good split, and the resulting split is good

– Thus the running time is (nlgn), but with a slightly larger constant

n

0 n - 1

(n-1)/2(n-1)/2-1

(n)n

(n-1)/2(n-1)/2

(n)

Page 16: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

16

Randomized QuickSort

• How do we increase the chance that all permutations are equally likely?– Random Sampling

• Don’t always use last element in subarray• Swap it with a randomly chosen element from the

subarray– Pivot now is equally likely to be any of the r – p + 1

elements• We can now expect the split to be reasonably well-

balanced on average

Page 17: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

17

Randomized QuickSort (cont)

Randomized-Partition( A, p, r )

1. i Random( p, r )

2. Exchange( A[ r ], A[ i ] )

3. return Partition( A, p, r )

Note that Partition( ) is same as before

Page 18: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

18

Randomized QuickSort (cont)

Randomized-QuickSort( A, p, r )

1. if p < r

2. q Randomized-Partition( A, p, r )

3. Randomized-QuickSort( A, p, q-1 )

4. Randomized-QuickSort( A, q+1, r )

Page 19: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

19

Analysis of QuickSort

• A more rigorous analysis• Begin with worst-case

– We intuited that worst-case running time is (n2)– Use substitution method to show this is true

)())1()((max)(10

nqnTqTnTnq

Page 20: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

20

Analysis of QuickSort (cont)

– Guess: (n2)– Show: for some c > 0– Substitute: 2)( cnnT

)()(

)()12()(

)())1((max

)())1((max)(

2

2

2

22

10

22

10

nnT

cn

nnccnnT

nqnqc

nqnccqnT

nq

nq

q2+(n-q-1)2 is max at endpoints of range. Therefore it is (n-1)2 = n2 – 2n +1

Page 21: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

21

Analysis of QuickSort (cont)

– Problem 7.4-1 has you show that

– Thus the worst-case running time of QuickSort is (n2)

)(

)())1()((max)(

2

10

n

nqnTqTnTnq

Page 22: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

22

Analysis of QuickSort (cont)

• We need to show that the upper-bound on expected running time is (nlgn)

• We’ve already shown that the best-case running time is (nlgn)

• Combined, these will give an expected running time of (nlgn)

Page 23: 1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place

23

Analysis of QuickSort (cont)• Expected Running Time

– Work done is dominated by Partition– Each time a pivot is selected, this element is

never included in subsequent calls to QuickSort• And the pivot is in its correct place in the array

– Therefore, at most n calls to Partition will be made

• Each call to Partition involves (1) work plus the amount of work done in the for loop

• Count the total number of times line 4 is executed, we can bound the amount of time spent in the for loop

Line 4: if A[j] x