You are on page 1of 21

QUICK SORT

1
University Institute of Engineering (UIE)
QUICK SORT

Developed by Hoare in 1960


Based on the Divide and Conquer Approach
Fastest known sorting algorithm in practice
Average case: O(N log N)
Best Case: O(N log N)
Worst case: O(N2)

2
University Institute of Engineering (UIE)
Divide and Conquer
Approach
Quick-sort is a sorting
algorithm based on the divide-
and-conquer paradigm: x
Divide: pick a random
element x (called pivot) and
partition S into
L elements less than x x
E elements equal x
G elements greater than x L E G
Recur: sort L and G
Conquer: join L, E and G
x
Base case : List of size 0 or 1.

University Institute of Engineering (UIE) 3


Algorithm for Quick Sort
Partition(A,p,r)
p:indicates initial value
r indicates final value 1. x=A[r]
q: indicates the position of 2. i=p-1
the pivot value
A: array 3. For j=p to r-1
Quicksort (A,p,r) 4. do if A[j]<=x
If (p<r) 5. then i=i+1
then q =partition 6. exchange A[i]<-> A[j]
(A,p,r) 7. exchange A[i+1]<-> A[r]
Quicksort(A,p,q-1) 8. return i+1
Quicksort(A,q+1,r)
Note : To sort an entire array A, the initial call is QUICKSORT(A, 1,
length[A])

University Institute of Engineering (UIE) 4


Partitioning

Partitioning : The key to the algorithm is the PARTITION


procedure, which rearranges the subarray A[pr] in
place.
Key step of quicksort algorithm
Goal: given the picked pivot, partition the remaining
elements into two smaller sets
Many ways to implement
Even the slightest deviations may cause surprisingly bad
results.
We will learn an easy and efficient partitioning strategy
here.
How to pick a pivot will be discussed later
5
University Institute of Engineering (UIE)
An Example of the Quick sort
Algorithm

Red colored box indicates pivot element


Green colored box indicates elements not processed
Pink colored indicates element less than pivot
Yellow colored indicates greater than pivot

2 8 7 1 3 5 6 4

6
University Institute of Engineering (UIE)
Example (1)

At any time this may be situation in the array


Red : pivot
Green: not processed
Yellow: greater than pivot
Pink :less than x

2 1 3 8 7 5 6 4

7
University Institute of Engineering (UIE)
Example(2) illustrates partition on an 8-element array

r r

2 8 7 1 3 5 6 4 2 1 3 8 7 5 6 4

p i j
p,j A
i i++ Pivot F

2 8 7 1 3 5 6 4 2 1 3 8 7 5 6 4

p,i j p i j
B
G
2 8 7 1 3 5 6 4
2 1 3 8 7 5 6 4
p,i j C p i
H
2 8 7 1 3 5 6 4

p,i j
i++ D 2 1 3 4 7 5 6 8

2 1 7 8 3 5 6 4 p i
Pivot
p i i++ j E I 8
University Institute of Engineering (UIE)
Small arrays

For very small arrays, quicksort does not perform as well


as insertion sort
how small depends on many factors, such as the time
spent making a recursive call, the compiler, etc
Do not use quicksort recursively for small arrays
Instead, use a sorting algorithm that is efficient for small
arrays, such as insertion sort

9
University Institute of Engineering (UIE)
Analysis

Assumptions:
A pivot at the last element of the array.
Running time
pivot selection: constant time O(1)
partitioning: linear time (n) where n=r-p+1
running time of the two recursive calls
T(N)=T(i)+T(N-i)+cN where c is a constant
i: number of elements in L
Rest n-i present in G or vice versa

10
University Institute of Engineering (UIE)
Worst-case Running Time
The worst case for quick-sort occurs when the pivot is
the unique minimum or maximum element
One of L and G has size n - 1 and the other has size 0
The running time is proportional to the sum
n + (n - 1) + + 2 + 1
Thus, the worst-case running time of quick-sort is O(n2)

depth time
0 n

1 n-1

n-1 1 11
University Institute of Engineering (UIE)
Picking the Pivot

Random number
Median of the array
Median of the three

12
University Institute of Engineering (UIE)
Pivot : Any Random

Use the first or last element as pivot


if the input is random, ok
if the input is presorted (or in reverse order)
all the elements go into L or G.
this happens consistently throughout the recursive calls
Results in O(n2) behavior
Choose the pivot randomly
generally safe
random number generation can be expensive

13
University Institute of Engineering (UIE)
Pivot : The Median of Array

Use the median of the array


Partitioning always cuts the array into roughly half
An optimal quick sort (O(N log N))
However, hard to find the exact median
e.g., sort an array to pick the value in the middle

14
University Institute of Engineering (UIE)
Pivot: median of three
\We will use median of three
Compare just three elements: the leftmost, rightmost and center
Swap these elements if necessary so that
A[left] = Smallest
A[right] = Largest
A[center] = Median of three

Pick A[center] as the pivot

15
University Institute of Engineering (UIE)
Pivot: median of three

A[left] = 2, A[center] = 13,


2 5 6 4 13 3 12 19 6
A[right] = 6

2 5 6 4 6 3 12 19 13 Swap A[center] and A[right]

2 5 6 4 6 3 12 19 13 Choose A[center] as pivot

pivot

3 12

16
University Institute of Engineering (UIE)
Expected Running Time
Consider a recursive call of quick-sort on a sequence of size s
Good call: the sizes of L and G are each less than 3s/4
Bad call: one of L and G has size greater than 3s/4
7 2 9 43 7 6 19 7 2 9 43 7 6 1

2 4 3 1 7 9 7 1 1 1 7294376

Good call Bad call

A call is good with probability 1/2


1/2 of the possible pivots cause good calls:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Bad pivots Good pivots Bad pivots

University Institute of Engineering (UIE) 17


Summary of Sorting Algorithms

Algorithm Time Notes


in-place
selection-sort O(n2) slow (good for small inputs)

in-place
insertion-sort O(n2) slow (good for small inputs)

O(n log n) in-place, randomized


quick-sort fastest (good for large inputs)
expected

in-place
heap-sort O(n log n) fast (good for very large inputs)

sequential data access


merge-sort O(n log n) fast (good for huge inputs)

University Institute of Engineering (UIE) 18


Problem

Suppose the the partitioning algorithm


Produces a 9 to 1 proportional spilt. The recurrence
relation obtained is:

T(n)<= T(9n/10) + T(n/10) + cn

Prove that the Complexity is O(nlgn )

19
University Institute of Engineering (UIE)
References
Introduction to Algorithms
Thomas H. Cormen,
Charles E. Leiserson
Ronald L. Rivest
Data Structures Using C and C++
Andrew S. Tanenbaum
www.utdallas.edu/~daescu/QuickSort.ppt
http://en.wikipedia.org/wiki/Quicksort

20
University Institute of Engineering (UIE)
Thanks

21
University Institute of Engineering (UIE)

You might also like