Professional Documents
Culture Documents
Lec 9+10 Divide and Conqure Quick Sort Algorithm
Lec 9+10 Divide and Conqure Quick Sort Algorithm
To sort an array S
1. If the number of elements in S is 0 or 1,
then return.
2. Pick any element V in S. This is calle !i"ot.
#. Partition S$%V& 'the remaining elements in
S( into t)o is*oint grou!s+
S
1
, %- S . %V& / - 0, V&,
S
2
, %- S . %V& / - 1, V&
2. 3eturn %4uickSort'S
1
( follo)e by "
follo)e by 4uickSort'S
2
( &
Quick Sort Algorithm
Quick Sort Algorithm
QuickSort'A, left, right( {
if (right 1 left( then {
!i"ot , Partition'A, left, right(5
QuickSort'A, left, !i"ot$1(5
QuickSort'A, !i"ot61, right(5
&
&
Quick Sort Partitioning
Quick Sort Partitioning
Picking the Pivot:
A , 7, 1, 2, 8, 9, #, :, 2, ;, 0
=
= + =
n
i
n O i c T n T
2
2
) ( ) 1 ( ) (
>est$=ase Analysis of Quick
>est$=ase Analysis of Quick
Sort
Sort