Professional Documents
Culture Documents
Winter 2022/2023
Anja Rey
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7316452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7316452
1234567
19/44 Advanced Programming and Algorithms I – Introduction Anja Rey
Insertion Sort
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7316452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7316452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7316452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7316452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
7716452
3
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
3716452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
3716452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
3716452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
3716452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
13376452
19/44 Advanced Programming and Algorithms I – Introduction Anja Rey
Insertion Sort
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1376452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1376452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1376452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1376452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1367452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1367452
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1346752
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1346752
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1345672
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1345672
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1234567
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1234567
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1376452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1376452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1 376452
3 i Ωj ≠1
while i Æ 0 and A[i] > key do
4
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n): 24
0 1 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1376452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1
24 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1377452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1377452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1377452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n):
0 1 2 3 4 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1367452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i] key 6
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
Algorithm:
insertion_sort(A[0..n ≠ 1], n): 34 44
0 1 2 5 6
1 for j Ω 1 to n ≠ 1 do
2 key Ω A[j] 1367452
3 i Ωj ≠1
4 while i Æ 0 and A[i] > key do
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
7 A[i + 1] Ω key
Computation problem:
Sorting
input: finite sequence of elements with a Æ relation, Èa1 , a2 , . . . , an Í
output: ordered sequence: permutation Èa1Õ , a2Õ , . . . , anÕ Í such that a1Õ Æ a2Õ Æ · · · Æ anÕ
1 import time
2
1 import time
2
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1 1
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1 1
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1 1
2 for i Ω 2 to n do
3 x Ωx ·i 1
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1 1
for i Ω 2 to n do 3
1 h
2
i
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1
2
4
for i Ω 2 to n do
x Ωx ·i
return x
i 4
I I k
21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Computation Model
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1 1
for i Ω 2 to n do
1 1 11
2
i 5
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1
Kid
2 for i Ω 2 to n do
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
factorial(n): instance n 4
1 x Ω1
for i Ω 2 to n do
Äh
2
3 x Ωx ·i
4 return x
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
3 x Ωx ·i 4 1
return x
4
1 1
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
1 x Ω1 13 steps
2 for i Ω 2 to n do
insertions_sort([3, 2, 1], 3)
3 x Ωx ·i
return x
4
23 steps
21/44 Advanced Programming and Algorithms I – Efficiency Anja Rey
Computation Model
Machine model:
Random Access Machine (RAM)
• one instruction in pseudocode takes one time step
• repeat instruction r times: r time steps
1 x Ω1
2 for i Ω 2 to n do
insertions_sort([3, 2, 1], 3)
3 x Ωx ·i
4 return x
Theoretical analysis:
• analyse time as a function T (n) of the input size n
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 d n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n d
4
5
while i Æ 0 and A[i] > key do
A[i + 1] Ω A[i] always true for ie 90 j d
6 i Ωi ≠1
A[i + 1] Ω key
7
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1
Für
4 while i Æ 0 and A[i] > key do
c
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
A[i + 1] Ω key
7
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] u 1
3 i Ωj ≠1
while i Æ 0 and A[i] > key do
c
En
4
5 A[i + 1] Ω A[i]
6 i Ωi ≠1
A[i + 1] Ω key
7
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3 i Ωj ≠1 n 1
F2
while i Æ 0 and A[i] > key do
ja
4
wie
5 A[i + 1] Ω A[i]
6
22/44
i Ωi ≠1
A[i + 1] Ω key
Advanced Programming and Algorithms I – Efficiency
n
we
d E Anja Rey
Running Time II
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
11
3 i Ωj ≠1 n d n n
YYFtu
3w.cn
while i Æ 0 and A[i] > key do
2j n
4
wie
5 A[i + 1] Ω A[i]
nu
6
22/44
i Ωi ≠1
A[i + 1] Ω key
Advanced Programming and Algorithms I – Efficiency
n 1
2
Eij Anja Rey
Running Time II
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
reverse order
insertion_sort(A[0..n ≠ 1], n):
1 for j Ω 1 to n ≠ 1 do n 1 1 n
2 key Ω A[j] 4 1
3
4
i Ωj ≠1
while i Æ 0 and A[i] > key do
ü Eig 945min
5 A[i + 1] Ω A[i]
6
22/44
i Ωi ≠1
A[i + 1] Ω key
Advanced Programming and Algorithms I – Efficiency
d
2
FIT
RE N
we
Anja Rey
Running Time II
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
Tu 2h43m 4
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
Tu 2h43m 4
time on Computer 1 2m13n 4 ca
time on Computer 2 2nd In 4 cz
22/44 Advanced Programming and Algorithms I – Efficiency
n En 2 23 Anja Rey
Running Time II
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
• asymptotic growth: n æ infty
Theoretical analysis:
• analyse time as a function T (n) of the input size n
• worst-case analysis: find upper bounds (guarantee for all inputs, max over all size n instances)
• asymptotic analysis: ignore constants (may become twice as fast in the future or on another
machine, already ignore differences in operations)
• asymptotic growth: n æ infty
MTU
Defintion: O-Notation
gut Let f , g : N æ R>0 .
In
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
Defintion: O-Notation
Let f , g : N æ R>0 .
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
Defintion: O-Notation
Let f , g : N æ R>0 .
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
Defintion: O-Notation
Let f , g : N æ R>0 .
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
Defintion: O-Notation
Let f , g : N æ R>0 .
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
flu
Defintion: O-Notation
gut Let f , g : N æ R>0 .
d
n
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
Defintion: O-Notation
Let f , g : N æ R>0 .
In Tu 2h43m 4 e 049
Intuition: g(n) œ O(f (n)) means that g(n) grows at most as fast as f (n) for n æ Œ.
J
Space Complexity
• memory needed to store information in the algorithm
• worst case: upper bound
• average case: all possible inputs of one size
æ see Exercise 2
Space Complexity
• memory needed to store information in the algorithm
• worst case: upper bound
• average case: all possible inputs of one size
æ see Exercise 2
Energy Complexity
• energy consumed by the algorithm
• comparably new research field in theory
• Python is rather inefficient.. TODO
Next week: