Quicksort
Quicksort
Quicksort
Lecture 24
Recursive sorting II
FIT 1008&2085
Introduction to Computer Science
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Cop yright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further reproduction o
communication of this material by you may be the subject of copyright protection under the Act.
Do not remove this notice.
Overview
• To review what a “divide and conquer” algorithm is
2
Divide and Conquer: Sorting
Combine
3
Divide and Conquer: Sorting
General Idea
5
Quick Sort
• Partition the list
• Sort the first part (recursively)
• Sort the second part (recursively)
• (Combine: there is nothing to do!)
6
Partition
• Choose an item in the list, called it the pivot.
• The first part consists of all those items which
are less than the pivot.
• The second part consists of all those items
larger than or equal to the pivot (except the
pivot). so we need to do some shuffling around to organise the data after we have chosen a pivot
7
after doing the partition we have all the data less than the pivot, the pivot, then all the data greater than the pivot then we recursively sort each section based on this partition.
why the combination step is trivial as every element in the first section of the partition is smaller than every element in the second section of the partition; this renders the merge operation as trivial.
Partition
x<p p p <= x
Sort should read x >= p
Sort
E) Something else.
9
Given a list of size N, how efficiently can the median be
computed?
A) O(1)
B) O(log N) the calculate the median element of a list it is a O(N) operation as we need to look at each part of the data in the input set
C) O(N)
D) O(N log N)
In practise we might not pick exactly the median each time we want to divide as this will be come costly
instead we can choose an element that will give approximately.a half-and-half split
thus we can pick the median of a subset of the lists elements
E) O(N^2)
11
Example Partition
array: 5 89 35 14 24 15 37 13 20 7 70
start:0 end:10
13
Example Partition
array: 5 89 35 14 24 15 37 13 20 7 70
14
Example Partition
array: 5 89 35 14 24 15 37 13 20 7 70
partition:
5 89 35 14 24 15 37 13 20 7 70
result
7 14 5 13 15 35 37 89 20 24 70
pivot position: 4
note that the pivot defines the boundaries
sort first half (using QS), 15sort second half (using QS)
Quicksort
16
Quicksort
17
p
swap with first element
p
index k
p x<p p >= x
index increases if necessary index k always increases
p x<p p >= x
x<p p p >= x
18
Example Partition
randomly pick element in position 5
array: 5 89 35 14 24 15 37 13 20 7 70
15 89 35 14 24 5 37 13 20 7 70
19
Example Partition
15 89 35 14 24 5 37 13 20 7 70
k:1
20
Example Partition
index:0
15 89 35 14 24 5 37 13 20 7 70
k:1
21
Example Partition
index:0
15 89 35 14 24 5 37 13 20 7 70
k:2
22
Example Partition
index:0
15 89 35 14 24 5 37 13 20 7 70
k:3
23
Example Partition
index:1
15 14
89 35 89
14 24 5 37 13 20 7 70
k:3
24
Example Partition
index:1
15 89
14 35 89 24 5 37 13 20 7 70
k:4
25
Example Partition
index:1
15 89
14 35 89 24 5 37 13 20 7 70
k:5
26
Example Partition
index:2
15 89
14 5 89 24 35 37 13 20 7 70
k:5
27
Example Partition
index:2
15 89
14 5 89 24 35 37 13 20 7 70
k:6
28
Example Partition
index:2
15 89
14 5 89 24 35 37 13 20 7 70
k:7
etc…
29
Example Partition
index:4
15 89
14 5 13 7 35 37 89 20 24 70
k:11
30
Example Partition
index:4
15
7 89
14 5 13 15
7 35 37 89 20 24 70
x < 15 x >= 15
31
Example Partition
7 14 5 13 15 35 37 89 20 24 70
quick_sort quick_sort
7 5 13 14 20 24 35 37 70 89
32
p
swap with first element
p
index k
p x<p p >= x
index
p x<p p >= x
x<p p p >= x
33
34
37
Found an element
that belongs in 1…
index
swap and update
index to maintain
invariant
38
What is the time complexity of the partition method?
A) O(log N)
B) O(N)
C) O(N log N)
D) O(N2)
39
What is the best-case time complexity of quicksort?
A) O(log N)
B) O(N)
C) O(N log N)
D) O(N2)
41
Quicksort: Number of partitions depends on the pivot
p
Partition
x<p p p <= x
pivot
n
height n/2 n/2
n
is
n/4 n/4 n/4 n/4
O(log
n
n)
n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8
44
Quick sort’s worst case
n
partition is O(n) n
n-1
n-1
n-2
n-2
n-3
n-3
...
n-4
n (n+1)
_______
2
45
Summary
• Merge Sort
• Easy: Split
• Elaborate: merge method
• Quick Sort
• Elaborate split: partition method
• Easy combination
52