Escolar Documentos
Profissional Documentos
Cultura Documentos
CS-E3190
Pekka Orponen
Department of Computer Science
Aalto University
Autumn 2017
Lecture 4: Divide-and-conquer II
CE-3190 PAT
Autumn 2017
2/19
4.1 Divide-and-conquer sorting
CE-3190 PAT
Autumn 2017
3/19
Quicksort
While mergesort is an excellent algorithm, in practice the
quicksort method (T. Hoare 1959) runs significantly faster, and
is the method of choice in software libraries.
CE-3190 PAT
Autumn 2017
4/19
A Python implementation of quicksort:
CE-3190 PAT
Autumn 2017
5/19
def partition(A, i, j, v):
l, r = i, j
while l < r:
while (A[l] <= v) and (l < r):
l = l + 1
while (A[r] > v) and (l < r):
r = r - 1
if l < r:
A[l], A[r] = A[r], A[l]
return l
CE-3190 PAT
Autumn 2017
6/19
Complexity of quicksort
I When the pivot elements are selected uniformly at random,
as in the implementation presented here, quicksort is a
randomised algorithm.1
I The runtime depends on the success of the pivoting:
typically a randomly chosen pivot partitions its interval in
roughly equal-size halves, but in the worst case the pivot
falls at either end of the interval, and the algorithm makes
slow progress.
I The worst-case runtime of quicksort is (n2 ). However the
probability of such unlucky pivoting is very low, and the
algorithm behaves according to its expected runtime of
O(n log n).
I Runtime analysis of quicksort is discussed at the tutorials,
and also later at Lecture 12 on Randomised Algorithms.
1
There are also deterministic pivoting rules, such as
median-of-first-three-elements, but we shall forego discussing them.
CE-3190 PAT
Autumn 2017
7/19
A lower bound on sorting (1/3)
CE-3190 PAT
Autumn 2017
8/19
A lower bound on sorting (2/3)
For a proof of this result, consider a comparison-based sorting
algorithm A and a comparison tree T that describes the pairwise
element comparisons A makes on any input sequence of length n,
where all the elements are distinct.
a1 < a2?
NO YES
NO YES NO YES
3 2 1 2 3 1 3 1 2 1 3 2
CE-3190 PAT
Autumn 2017
9/19
A lower bound on sorting (3/3)
I Now consider every possible input ordering (permutation) to
the algorithm A, trace the comparisons A makes on this
ordering, and label the corresponding leaf of T with .
I Then every permutation of n elements appears on some leaf
of T , and no leaf contains two permutations. (Because
otherwise A would sort two permutations 6= 0 in exactly the
same way, which would necessarily be wrong for at least one of
them.) There are n! permutations of n elements, so T has at
least n! leaves.
I Now T is a binary tree, so if it has height (maximum path-length)
h, then it can have at most 2h leaves. Conversely, since T has
n! leaves, T must have height log2 (n!) = (n log n).
[Tutorial 1.]
I Hence there is some input sequence on which algorithm A
performs (n log n) pairwise comparisons of elements.
CE-3190 PAT
Autumn 2017
10/19
Some visualisations of sorting algorithms
http://www.sorting-algorithms.com
CE-3190 PAT
Autumn 2017
11/19
4.2 Divide-and-conquer search
The best-known divide-and-conquer search algorithm is surely
binary search.
CE-3190 PAT
Autumn 2017
12/19
Complexity of binary search
CE-3190 PAT
Autumn 2017
13/19
Divide-and-conquer selection
CE-3190 PAT
Autumn 2017
14/19
Quickselect
An application of the quicksort idea to the selection problem.
I Sv = [x S | x = v ]
I SR = [x S | x > v ]
If k |SL | then return Q UICK S ELECT(SL , k ).
If |SL | < k |SL | + |Sv | then return v .
If k > |SL | + |Sv | then return Q UICK S ELECT(SR , k |SL | |Sv |).
a
This can be done similarly in situ within S as in quicksort.
CE-3190 PAT
Autumn 2017
15/19
Complexity of quickselect
I Similarly as quicksort, also quickselect is a randomised
algorithm. The actual runtime depends on the success of
pivoting.
I Worst-case runtime T (n) = (n2 ).
I Expected runtime T (n) = O(n).
I Heuristically, a random pivot falls typically in the middle of
CE-3190 PAT
Autumn 2017
17/19
Analysis of deterministic quickselect (1/2)
How uneven can the partition between SL and SR be now?
M
<= v
>= v
CE-3190 PAT
Autumn 2017
19/19