quick sort pp t 2114
TRANSCRIPT
-
7/26/2019 Quick Sort Pp t 2114
1/33
Quick Sort
Divide: Partition the array into two sub-arrays
A[p . . q-1] and A[q+1 . . r] such that each element o
A[p . . q-1] is less than or equal to A[q]! which in turn
less than or equal to each element o A[q+1 . . r]
-
7/26/2019 Quick Sort Pp t 2114
2/33
Quick Sort
"onquer: #ort the two sub-arrays A[p . . q-1] and
A[q+1 . . r] by recursive calls to quic$ sort.
-
7/26/2019 Quick Sort Pp t 2114
3/33
Quick Sort
"ombine: #ince the sub-arrays are sorted in place! no
wor$ is needed to combine them.
-
7/26/2019 Quick Sort Pp t 2114
4/33
Quick Sort
%&'"(#)*,A! p! r
i p r
then qPA*'')/,A! p! r
%&'"(#)*,A! p! q-1
%&'"(#)*,A! q+1! r
-
7/26/2019 Quick Sort Pp t 2114
5/33
Quick Sort
PA*'')/,A! p! r
0A[r]
ip-1
-
7/26/2019 Quick Sort Pp t 2114
6/33
Quick Sort
or
p to r-1
do i A[] 2 0
then i
i+1
e0chan3e A[i]A[]
e0chan3e A[i+1]
A[r]
return i+1
-
7/26/2019 Quick Sort Pp t 2114
7/33
Quick Sort
i p, j r
2 8 7 1 3 5 6 4
(a)
-
7/26/2019 Quick Sort Pp t 2114
8/33
Quick Sort
p, i j r
2 8 7 1 3 5 6 4
(b)
-
7/26/2019 Quick Sort Pp t 2114
9/33
Quick Sort
p, i j r
2 8 7 1 3 5 6 4
(c)
-
7/26/2019 Quick Sort Pp t 2114
10/33
Quick Sort
p, i j r
2 8 7 1 3 5 6 4
(d)
-
7/26/2019 Quick Sort Pp t 2114
11/33
Quick Sort
p i j r
2 1 7 8 3 5 6 4
(e)
-
7/26/2019 Quick Sort Pp t 2114
12/33
Quick Sort
p i j r
2 1 3 8 7 5 6 4
(f)
-
7/26/2019 Quick Sort Pp t 2114
13/33
Quick Sort
p i j r
2 1 3 8 7 5 6 4
(g)
-
7/26/2019 Quick Sort Pp t 2114
14/33
Quick Sort
p i r
2 1 3 8 7 5 6 4
(h)
-
7/26/2019 Quick Sort Pp t 2114
15/33
Quick Sort
p i r
2 1 3 4 7 5 6 8
(i)
-
7/26/2019 Quick Sort Pp t 2114
16/33
Quick Sort
4orst-case partitionin3:
he partitionin3 routine produces one sub-problem
with n-1 elements and another sub-problem with 5
elements. #o the partitionin3 costs 6,n time.
-
7/26/2019 Quick Sort Pp t 2114
17/33
Quick Sort
4orst-case partitionin3:
he recurrence or the runnin3 time
,n2 ,n-1 + ,5 + 6,n
2,n-1 + 6,n
2----------------- 6,n7
-
7/26/2019 Quick Sort Pp t 2114
18/33
Quick Sort
4orst-case partitionin3:
he 6,n7 runnin3 time occurs when the input
array is already completely sorted 8 a common
situation in which insertion sort runs in O,n time
-
7/26/2019 Quick Sort Pp t 2114
19/33
Quick Sort
9est-case partitionin3:
he partitionin3 procedure produces two
sub-problems! each o sie not more than
n;7.
-
7/26/2019 Quick Sort Pp t 2114
20/33
Quick Sort
9est-case partitionin3:
he recurrence or the runnin3 time
,n 2 7,n;7 + 6,n
2 ----- O,n l3 n
-
7/26/2019 Quick Sort Pp t 2114
21/33
Quick Sort
9est-case partitionin3:
he equal balancin3 o the two sides o the
partition at every level o the recursion
produces aster al3orithm.
-
7/26/2019 Quick Sort Pp t 2114
22/33
Quick Sort
9alanced partitionin3:
#uppose! the partitionin3 al3orithm always
produces
-
7/26/2019 Quick Sort Pp t 2114
23/33
Quick Sort
9alanced partitionin3:
he recurrence or the runnin3 time
,n 2 ,
-
7/26/2019 Quick Sort Pp t 2114
24/33
Quick Sort
9alanced partitionin3: he recursion tree
-
7/26/2019 Quick Sort Pp t 2114
25/33
Quick Sort
9alanced partitionin3:
'n act! a
-
7/26/2019 Quick Sort Pp t 2114
26/33
Quick Sort
'ntuition or the avera3e case:
't is unli$ely that the partitionin3 always happens
in the same way at every level.
-
7/26/2019 Quick Sort Pp t 2114
27/33
Quick Sort
'ntuition or the avera3e case:
'n the avera3e case! PA*')/ produces a mi0 o
=3ood> and =bad> splits.
-
7/26/2019 Quick Sort Pp t 2114
28/33
Quick Sort'ntuition or the avera3e case:
he combination o the bad split ollowed by the 3ood split
produces three arrays o sies 5! ,n-1;7-1! and ,n-1;7 at a
combined partitionin3 cost o 6,n + 6,n-12 6,n
n
n1
(n1)!21 (n1)!2
"
#(n)
-
7/26/2019 Quick Sort Pp t 2114
29/33
Quick Sort'ntuition or the avera3e case:
A sin3le level o partitionin3 produces two sub-arrays o sie
,n-1;7 at a cost o 6,n.
n
(n1)!2 (n1)!2
#(n)
-
7/26/2019 Quick Sort Pp t 2114
30/33
$ %ando&i'ed erion of Quick Sort
'nstead o always usin3 A[r] as the pivot! we will
use a randomly chosen element rom the sub-array
A[p..r].
-
7/26/2019 Quick Sort Pp t 2114
31/33
$ %ando&i'ed erion of Quick Sort
9ecause the pivot element is randomly chosen!
we e0pect the split o the input array to be
reasonably well balanced on avera3e.
-
7/26/2019 Quick Sort Pp t 2114
32/33
$ %ando&i'ed erion of Quick Sort
*A/D)?'@D-PA*'')/,A! p! r
i*A/D)?,p! r
e0chan3e A[r]A[i]
return PA*'')/,A! p! r
-
7/26/2019 Quick Sort Pp t 2114
33/33
$ %ando&i'ed erion of Quick Sort
*A/D)?'@D-%&'"(#)*,A! p! r
i pr then
q*A/D)?'@D-PA*'')/,A! p! r
*A/D)?'@D-%&'"(#)*,A! p! q-1
*A/D)?'@D-%&'"(#)*,A! q+1! r