Notes 03 Sorting PDF
Notes 03 Sorting PDF
Sebastian Wild
18 February 2020
3 Efficient Sorting
3.1 Mergesort
3.2 Quicksort
3.3 Comparison-Based Lower Bound
3.4 Integer Sorting
3.5 Parallel computation
3.6 Parallel primitives
3.7 Parallel sorting
Why study sorting?
� fundamental problem of computer science that is still not solved
Algorithm with optimal #comparisons in worst case?
� building brick of many more advanced algorithms
� for preprocessing
� as subroutine
Here:
� “classic” fast sorting method
� parallel sorting
1
Part I
The Basics
Rules of the game
� Given:
� array 𝐴[0..𝑛 − 1] of 𝑛 objects
� a total order relation ≤ among 𝐴[0], . . . , 𝐴[𝑛 − 1]
(a comparison function)
2
3.1 Mergesort
Clicker Question
E Don’t know.
pingo.upb.de/622222
3
Clicker Question
E Don’t know.
pingo.upb.de/622222
3
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Merging sorted lists
4
Mergesort
1 procedure mergesort(𝐴[𝑙..𝑟])
2 𝑛 := 𝑟 − 𝑙 + 1 � recursive procedure; divide & conquer
3 if 𝑛 ≥ 1 return
� � � merging needs
4 𝑚 := 𝑙 + 𝑛2
� temporary storage for result
5 mergesort(𝐴[𝑙..𝑚 − 1])
of same size as merged runs
6 mergesort(𝐴[𝑚..𝑟])
7 merge(𝐴[𝑙..𝑚 − 1], 𝐴[𝑚..𝑟], buf ) � to read and write each element twice
8 copy buf to 𝐴[𝑙..𝑟] (once for merging, once for copying back)
5
Mergesort
1 procedure mergesort(𝐴[𝑙..𝑟])
2 𝑛 := 𝑟 − 𝑙 + 1 � recursive procedure; divide & conquer
3 if 𝑛 ≥ 1 return
� � � merging needs
4 𝑚 := 𝑙 + 𝑛2
� temporary storage for result
5 mergesort(𝐴[𝑙..𝑚 − 1])
of same size as merged runs
6 mergesort(𝐴[𝑚..𝑟])
7 merge(𝐴[𝑙..𝑚 − 1], 𝐴[𝑚..𝑟], buf ) � to read and write each element twice
8 copy buf to 𝐴[𝑙..𝑟] (once for merging, once for copying back)
Simplification 𝑛 = 2 𝑘
�
0 𝑘≤0
𝐶(2 𝑘 ) = = 2 · 2 𝑘 + 22 · 2 𝑘−1 + 23 · 2 𝑘−2 + · · · + 2 𝑘 · 21 = 2𝑘 · 2 𝑘
2 · 𝐶(2 𝑘−1
)+2·2 𝑘
𝑘≥1
𝐶(𝑛) = 2𝑛 lg(𝑛) = Θ(𝑛 log 𝑛)
5
and for arbitrary 𝑛 we have 𝐶(𝑛) ≤ 𝐶(next larger power of 2) ≤ 4𝑛 lg(𝑛) + 2𝑛 = Θ(𝑛 log 𝑛)
Mergesort – Discussion
optimal time complexity of Θ(𝑛 log 𝑛) in the worst case
6
3.2 Quicksort
Clicker Question
E Don’t know.
pingo.upb.de/622222
7
Clicker Question
E Don’t know.
pingo.upb.de/622222
7
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
<𝑝
8
Partitioning around a pivot
�
<𝑝
8
Partitioning around a pivot
<𝑝
8
Partitioning around a pivot
<𝑝 >𝑝
8
Partitioning around a pivot
<𝑝
✗
>𝑝
8
Partitioning around a pivot
<𝑝
✗
>𝑝
8
Partitioning around a pivot
<𝑝
✗
>𝑝
�
>𝑝
8
Partitioning around a pivot
<𝑝
✗
>𝑝 >𝑝
8
Partitioning around a pivot
<𝑝
✗
>𝑝
✗
<𝑝 >𝑝
8
Partitioning around a pivot
<𝑝
✗
>𝑝
✗
<𝑝 >𝑝
8
Partitioning around a pivot
<𝑝
�
<𝑝
�
>𝑝 >𝑝
8
Partitioning around a pivot
8
Partitioning around a pivot
<𝑝 <𝑝
�
<𝑝 >𝑝 >𝑝
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning around a pivot
8
Partitioning – Detailed code
Beware: details easy to get wrong; use this code!
1 procedure partition(𝐴, 𝑏)
2 // input: array 𝐴[0..𝑛 − 1], position of pivot 𝑏 ∈ [0..𝑛 − 1]
3 swap(𝐴[0], 𝐴[𝑏])
4 𝑖 := 0, 𝑗 := 𝑛
5 while true do
6 do 𝑖 := 𝑖 + 1 while 𝑖 < 𝑛 and 𝐴[𝑖] < 𝐴[0]
7 do 𝑗 := 𝑗 − 1 while 𝑗 ≥ 1 and 𝐴[𝑗] > 𝐴[0]
8 if 𝑖 ≥ 𝑗 then break (goto 8)
9 else swap(𝐴[𝑖], 𝐴[𝑗])
10 end while
11 swap(𝐴[0], 𝐴[𝑗])
12 return 𝑗
10
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 9 8
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6 8 9
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6 8 9
1 2 3 5 6 8
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6 8 9
1 2 3 5 6 8
1 3 6
11
Quicksort & Binary Search Trees
Quicksort
7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6 8 9
1 2 3 5 6 8
1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8
2 1 3 4 5 6 8 9
1 2 3 5 6 8
1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9
1 2 3 5 6 8
1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4
1 2 3 5 6 8
1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4
1 2 3 5 6 8 2
1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2
1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2
1 3 6 1
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2
1 3 6 1 3
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2 8
1 3 6 1 3
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2 5 8
1 3 6 1 3
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2 5 8
1 3 6 1 3 6
11
Quicksort & Binary Search Trees
Quicksort Binary Search Tree (BST)
7 4 2 9 1 3 8 5 6 7 4 2 9 1 3 8 5 6
4 2 1 3 5 6 7 9 8 7
2 1 3 4 5 6 8 9 4 9
1 2 3 5 6 8 2 5 8
1 3 6 1 3 6
11
Quicksort – Worst Case
� Problem: BSTs can degenerate 1
�𝑛 𝑛(𝑛 − 1) 1 2
3
� Total cost 𝑘=1 (𝑘 − 1) = ∼ 2𝑛 4
2
5
terribly slow!
12
Randomized Quicksort – Analysis
� 𝐶(𝑛) = element visits (as for mergesort)
13
Quicksort – Discussion
fastest general-purpose method
14
3.3 Comparison-Based Lower Bound
Lower Bounds
� Lower bound: mathematical proof that no algorithm can do better.
� very powerful concept: bulletproof impossibility result
≈ conservation of energy in physics
� (unique?) feature of computer science:
for many problems, solutions are known that (asymptotically) achieve the lower bound
� can speak of “optimal algorithms”
15
Lower Bounds
� Lower bound: mathematical proof that no algorithm can do better.
� very powerful concept: bulletproof impossibility result
≈ conservation of energy in physics
� (unique?) feature of computer science:
for many problems, solutions are known that (asymptotically) achieve the lower bound
� can speak of “optimal algorithms”
� To prove a statement about all algorithms, we must precisely define what that is!
15
The Comparison Model
� In the comparison model data can only be accessed in two ways:
� comparing two elements
� moving elements around (e. g. copying, swapping)
16
The Comparison Model
� In the comparison model data can only be accessed in two ways:
� comparing two elements
� moving elements around (e. g. copying, swapping)
� This makes very few assumptions on the kind of objects we are sorting.
16
The Comparison Model
� In the comparison model data can only be accessed in two ways:
� comparing two elements
� moving elements around (e. g. copying, swapping)
� This makes very few assumptions on the kind of objects we are sorting.
16
Comparison Lower Bound
Example: Comparison tree for a sorting method for 𝐴[0..2]:
𝐴[0] : 𝐴[1]
≤ 1,2,3
1,2,3
1,3,2
> 2,1,3
1,3,2 3,1,2
2,1,3
2,3,1 3,2,1
2,3,1
3,1,2
𝐴[1] : 𝐴[2] 3,2,1 𝐴[1] : 𝐴[2]
≤ > ≤ >
1,3,2 2,1,3
2,3,1 3,1,2
≤ > ≤ >
17
Comparison Lower Bound
Example: Comparison tree for a sorting method for 𝐴[0..2]:
≤ > ≤
� comparison trees are binary trees
>
1,3,2 2,1,3
2,3,1 3,1,2 � ℓ leaves � height ≥ �lg(ℓ )�
1,2,3 𝐴[0] : 𝐴[2] 𝐴[0] : 𝐴[2] 3,2,1
� comparison trees for sorting
≤ > ≤ > method must have ≥ 𝑛! leaves
� height ≥ lg(𝑛!) ∼ 𝑛 lg 𝑛
1,3,2 2,3,1 2,1,3 3,1,2
more precisely: lg(𝑛!) = 𝑛 lg 𝑛 − lg(𝑒)𝑛 + 𝑂(log 𝑛)
17
Comparison Lower Bound
Example: Comparison tree for a sorting method for 𝐴[0..2]:
≤ > ≤
� comparison trees are binary trees
>
1,3,2 2,1,3
2,3,1 3,1,2 � ℓ leaves � height ≥ �lg(ℓ )�
1,2,3 𝐴[0] : 𝐴[2] 𝐴[0] : 𝐴[2] 3,2,1
� comparison trees for sorting
≤ > ≤ > method must have ≥ 𝑛! leaves
� height ≥ lg(𝑛!) ∼ 𝑛 lg 𝑛
1,3,2 2,3,1 2,1,3 3,1,2
more precisely: lg(𝑛!) = 𝑛 lg 𝑛 − lg(𝑒)𝑛 + 𝑂(log 𝑛)
A Yes B No
pingo.upb.de/622222
18
Clicker Question
A Yes � B No
pingo.upb.de/622222
18
3.4 Integer Sorting
How to beat a lower bound
� Does the above lower bound mean, sorting always takes time Ω(𝑛 log 𝑛)?
19
How to beat a lower bound
� Does the above lower bound mean, sorting always takes time Ω(𝑛 log 𝑛)?
19
How to beat a lower bound
� Does the above lower bound mean, sorting always takes time Ω(𝑛 log 𝑛)?
19
How to beat a lower bound
� Does the above lower bound mean, sorting always takes time Ω(𝑛 log 𝑛)?
19
Counting sort
� Important parameter: size/range of numbers
� numbers in range [0..𝑈) = {0, . . . , 𝑈 − 1} typically 𝑈 = 2𝑏 � 𝑏-bit binary numbers
� Can sort 𝑛 integers in range [0..𝑈) with 𝑈 = 𝑂(𝑛) in time and space Θ(𝑛).
20
Integer Sorting – State of the art
� 𝑂(𝑛) time sorting also possible for numbers in range 𝑈 = 𝑂(𝑛 𝑐 ) for constant 𝑐.
� radix sort with radix 2𝑤
� algorithm theory
� suppose 𝑈 = 2𝑤 , but 𝑤 can be arbitrary function of 𝑛
� how fast can we sort 𝑛 such 𝑤-bit integers on a 𝑤-bit word-RAM?
� for 𝑤 = 𝑂(log 𝑛): linear time (radix/counting sort)
� for 𝑤 = Ω(log2+𝜀 𝑛): linear time (signature sort)
�
� for 𝑤 in between: can do 𝑂(𝑛 lg lg 𝑛) (very complicated algorithm)
don’t know if that is best possible!
21
Integer Sorting – State of the art
� 𝑂(𝑛) time sorting also possible for numbers in range 𝑈 = 𝑂(𝑛 𝑐 ) for constant 𝑐.
� radix sort with radix 2𝑤
� algorithm theory
� suppose 𝑈 = 2𝑤 , but 𝑤 can be arbitrary function of 𝑛
� how fast can we sort 𝑛 such 𝑤-bit integers on a 𝑤-bit word-RAM?
� for 𝑤 = 𝑂(log 𝑛): linear time (radix/counting sort)
� for 𝑤 = Ω(log2+𝜀 𝑛): linear time (signature sort)
�
� for 𝑤 in between: can do 𝑂(𝑛 lg lg 𝑛) (very complicated algorithm)
don’t know if that is best possible!
∗ ∗ ∗
21
Part II
Sorting with of many processors
3.5 Parallel computation
Types of parallel computation
£££ can’t buy you more time, but more computers!
� Challenge: Algorithms for parallel computation.
2. distributed computing
� 𝑝 PEs working in parallel
� each PE has private memory
� communication by sending messages via a network
22
PRAM – Parallel RAM
� extension of the RAM model (recall Unit 1)
23
PRAM – Conflict management
Problem: What if several PEs simultaneously overwrite a memory cell?
� EREW-PRAM (exclusive read, exclusive write)
any parallel access to same memory cell is forbidden (crash if happens)
� time: number of steps till all PEs finish assuming sufficiently many PEs!
sometimes called depth or span
� work: total #instructions executed on all PEs
25
The number of processors
Hold on, my computer does not have Θ(𝑛) processors� Why should I care for span and work�?
Example:
input: 3 0 0 5 7 0 0 2 0 0 0 4 0 8 0 1
�
output: 3 3 3 8 15 15 15 17 17 17 17 21 21 29 29 30
27
Clicker Question
A 𝑂(𝑛 3 ) D 𝑂(𝑛)
√
B 𝑂(𝑛 2 ) E 𝑂( 𝑛)
pingo.upb.de/622222
28
Clicker Question
A 𝑂(𝑛 3 ) D 𝑂(𝑛) �
√
B 𝑂(𝑛 2 ) E 𝑂( 𝑛)
pingo.upb.de/622222
28
Prefix sums – Sequential
� sequential solution does 𝑛 − 1 additions
1 procedure prefixSum(𝐴[0..𝑛 − 1])
� but: cannot parallelize them 2 for 𝑖 := 1, . . . , 𝑛 − 1 do
data dependencies! 3 𝐴[𝑖] := 𝐴[𝑖 − 1] + 𝐴[𝑖]
29