14
Muhammad Sarwar QAU Islamabad 1 QUICK SORT 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 )

Ch 7 quick sort

Embed Size (px)

Citation preview

Page 1: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 1

QUICK SORTQUICKSORT (A, p, r)1. if p < r2. then q ← PARTITION(A, p, r)3. QUICKSORT(A,p, q-1 )4. QUICKSORT(A,q+1,r )

Page 2: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 2

PARTITION

PARTITION(A, p, r)1. x←A[r]2. i ← p-13. for j←p to r-14. if A[j]≤x then5. i←i+16. exchange A[i]↔a[j]7. exchange A[i+1]↔A[r]8. return i+1

Page 3: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 3

QUICK SORT

668822996040907755113344

668844996040907755113322

668855996040907744113322

668855996044907740113322

668855996077904440113322

668855996077904440113322

Page 4: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 4

PARTITIONPARTITION (A, p, r)1. x←A[p], i ←p-1, j ←r+12. while TRUE3. do repeat j ←j - 14. until A[j]≤ x5. repeat i ←i+16. until A[i]≥ x7. if i<j 8. then exchange A[i]↔A[j]9. else return j

Page 5: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 5

PARTITIONPARTITION (A, p, r)1. x←A[p], i ←p-1, j ←r+12. while TRUE3. do repeat j ←j - 14. until A[j]≤ x5. repeat i ←i+16. until A[i]≥ x7. if i<j 8. then exchange A[i]↔A[j]9. else return j

Page 6: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 6

PARTITION1. left←beg, right ← end, loc ← beg2. [SCAN FROM RIGHT TO LEFT]

a) repeat while a [loc]<=a [right] & loc!=rightright ← right-1

b) if loc=right then return locc) if A [loc]>A [right] then

i. interchange A [loc] & A [right]ii. loc ← right go to step 3

3. [SCAN FROM LEFT TO RIGHT]a) repeat while A [left]<=A [loc] & left!=loc

left ← left+1b) if loc=left then return locc) if A [left]>A [loc] then

i. interchange A [left] & A [loc]ii. loc ← left, go to step 2

Page 7: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 7

ANALYSIS OF QUICK SORT

• Running time of quick sort depends on whether the partitioning is balanced or unbalanced

• The balancing of partition depends on which elements are used

• If partitioning is balanced the running time of quick sort is as fast as merge sort

• If partitioning is unbalanced the algorithm runs as slow as insertion sort

• In worst case the partitions have size 1 & n-1• In best case the partitions have size n/2

Page 8: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 8

Page 9: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 9

ANALYSIS OF QUICK SORTWORST CASE

)()(2

)1()1()(

.....

.....)()1()2()3()(

)()1()2()()()1()(

21

nnT

nnknT

nnnnTnTnnnTnT

nnTnT

n

k

Θ=

+=+Θ=

Θ+−Θ+−Θ+−=Θ+−Θ+−=

Θ+−=

∑=

Page 10: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 10

ANALYSIS OF QUICK SORTBEST CASE

)lg()()lg()(

oremmaster the of 2 Case )(

)()(

2,2)()2/(2)(

log

log

2loglog 2

nnnTnnnT

nnf

nnfnnn

bannTnT

a

a

a

b

b

b

Θ=⇒Θ=⇒

=⇒

Θ===

==Θ+=

Page 11: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 11

QUICK SORT

• What value of q does PARTITION return when all elements in the array A[p…r] have the same value?

• q = ⎣(p+r)/2⎦

Page 12: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 12

QUICK SORT

Suppose the partitions are in n/10 and 9n/10 what is the running time?

We can make a tree to see the behavior

The recurrence is

T(n)=T(n/10)+T(9n/10)+Θ(n)

Page 13: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 13

Page 14: Ch 7 quick sort

Muhammad Sarwar QAU Islamabad 14

Q. Suppose that the splits at every level of quick sort are in proportion 1 – α to α where 0<α<=1/2 is a constant. Show that the minimum depth of a leaf in the recursion tree is approximately –lgn/lgα and maximum depth is approximately –lgn/lg(1 – α)

Ans:-If we expand the tree the side of α will end before the side of 1–α

)1lg(lg

)1(loglog

)1(log1loglog

)1/(1logloglog )1/(1

αα

ααα

−−=

−−=

−−=

−=−

nn

nnn

b

b

bb

b

b

b

ααααα lglg

loglog

log1loglog

/1logloglog /1

nnnnnb

b

bb

b

b

b −=−=−

==