Upload
muhammad-wasif
View
182
Download
2
Embed Size (px)
Citation preview
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 )
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
Muhammad Sarwar QAU Islamabad 3
QUICK SORT
668822996040907755113344
668844996040907755113322
668855996040907744113322
668855996044907740113322
668855996077904440113322
668855996077904440113322
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
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
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
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
Muhammad Sarwar QAU Islamabad 8
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
Θ=
+=+Θ=
Θ+−Θ+−Θ+−=Θ+−Θ+−=
Θ+−=
∑=
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
Θ=⇒Θ=⇒
=⇒
Θ===
==Θ+=
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⎦
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)
Muhammad Sarwar QAU Islamabad 13
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 −=−=−
==