Quick Sort
Quick Sort
Chapter 8
Kruse and Ryba
Sorting algorithms
Insertion, selection and bubble sort have
quadratic worst-case performance
The faster comparison based algorithm ?
O(nlogn)
Mergesort and Quicksort
Merge Sort
Apply divide-and-conquer to sorting problem
Problem: Given n elements, sort elements into
non-decreasing order
Divide-and-Conquer:
If n=1 terminate (every one-element list is already
sorted)
If n>1, partition elements into two or more subcollections; sort each; combine into a single sorted list
How do we partition?
Example
Partition into lists of size n/2
[10, 4, 6, 3, 8, 2, 5, 7]
[10, 4, 6, 3]
[8, 2, 5, 7]
[6, 3]
[8, 2]
[5, 7]
[2][8]
[5][7]
[10, 4]
Example Contd
Merge
[2, 3, 4, 5, 6, 7, 8, 10 ]
[3, 4, 6, 10]
[2, 5, 7, 8]
[3, 6]
[2, 8]
[5, 7]
[2][8]
[5][7]
[4, 10]
Merge Function
Evaluation
Recurrence equation:
Assume n is a power of 2
c1
if n=1
T(n) =
2T(n/2) + c2n
if n>1, n=2k
Solution
By Substitution:
T(n) = 2T(n/2) + c2n
T(n/2) = 2T(n/4) + c2n/2
T(n) = 4T(n/4) + 2 c2n
T(n) = 8T(n/8) + 3 c2n
T(n) = 2iT(n/2i) + ic2n
Assuming n = 2k, expansion halts when we get T(1) on right side; this
happens when i=k T(n) = 2kT(1) + kc2n
Since 2k=n, we know k=logn; since T(1) = c1, we get
T(n) = c1n + c2nlogn;
thus an upper bound for TmergeSort(n) is O(nlogn)
Quicksort Algorithm
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
Example
We are given array of n integers to sort:
40
20
10
80
60
50
30 100
20
10
80
60
50
30 100
Partitioning Array
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1.
2.
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
80
60
50
30 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
60
50
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
pivot_index = 0
40
20
10
30
50
60
80 100
too_small_index
40
20
10
30
50
60
80 100
too_small_index
20
10
30
40
50
60
80 100
too_small_index
Partition Result
7
20
10
30
40
50
60
80 100
> data[pivot]
20
10
30
40
50
60
80 100
> data[pivot]
Quicksort Analysis
Assume that keys are random, uniformly
distributed.
What is best case running time?
Quicksort Analysis
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Quicksort Analysis
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Quicksort Analysis
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Quicksort Analysis
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Quicksort Analysis
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
Quicksort Analysis
Quicksort Analysis
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
too_small_index
10
12
13
50
57
63 100
> data[pivot]
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Recursion:
1.
2.
Quicksort Analysis
Quicksort Analysis
Improving Performance of
Quicksort
Improved selection of pivot.
For sub-arrays of size 3 or less, apply brute
force search:
Sub-array of size 1: trivial
Sub-array of size 2:
if(data[first] > data[second]) swap them