03 Mergesort
03 Mergesort
Gustavo Carvalho
([email protected])
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 1 / 106
Selection sort (brute force)
Agenda
3 Mergesort (divide-and-conquer)
4 Bibliography
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 2 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 89 45 68 90 29 34 17
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 3 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 89 45 68 90 29 34 17
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 4 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 45 68 90 29 34 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 5 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 45 68 90 29 34 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 6 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 45 68 90 29 34 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 7 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 68 90 45 34 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 8 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 68 90 45 34 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 9 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 68 90 45 34 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 10 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 90 45 68 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 11 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 90 45 68 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 12 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 90 45 68 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 13 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 90 68 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 14 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 90 68 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 15 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 90 68 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 16 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 68 90 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 17 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 68 90 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 18 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 68 90 89
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 19 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 68 89 90
i
min
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 20 / 106
Selection sort (brute force)
Selection sort
Idea: find the smallest element and exchange it with the first one
index: 0 1 2 3 4 5 6
value: 17 29 34 45 68 89 90
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 21 / 106
Selection sort (brute force)
Cworst (n) =
Pn−2 Pn−1 Remember that:
i=0 j=i+1 1 Pu
Pn−2 i=l c ∗ ai
= i=0 ((n − 1) − (i + 1) + 1)
= P
c ∗ ui=l ai
Pn−2
= (n − 1 − i)
i=0
= i=0 (n − 1) − n−2
Pn−2 P
i=0 i
Pu
i=l (ai ± bi )
= (n − 1) i=0 1 − (n−2)(n−1)
Pn−2 =
2 Pu Pu
i=l ai ± i=l bi
(n−2)(n−1)
= (n − 1)2 − 2
n2 −n Sn = (a1 +a n )∗n
= 2 ∈ Θ(n2 ) 2
(arithm. prog.)
Regarding the #swaps: Sworst (n) = n − 1 ∈ Θ(n)
Bubble sort: C(n) ∈ Θ(n2 ), Sworst (n) ∈ Θ(n2 )
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 22 / 106
Insertion sort (decrease-and-conquer)
Agenda
3 Mergesort (divide-and-conquer)
4 Bibliography
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 23 / 106
Insertion sort (decrease-and-conquer)
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 24 / 106
Insertion sort (decrease-and-conquer)
Decrease-and-conquer
Design strategy: decrease-and-conquer
Decrease by a constant
Variable-size decrease
Example: gcd(m, n) = gcd(n, m mod n)
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 25 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 45
value: 89 45 68 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 26 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 45
value: 89 89 68 90 29 34 17 j = −1
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 27 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 45
value: 45 89 68 90 29 34 17 j = −1
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 28 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 68
value: 45 89 68 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 29 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 68
value: 45 89 89 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 30 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 68
value: 45 68 89 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 31 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 90
value: 45 68 89 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 32 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 90
value: 45 68 89 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 33 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 90
value: 45 68 89 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 34 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 29
value: 45 68 89 90 29 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 35 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 29
value: 45 45 68 89 90 34 17 j = −1
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 36 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 29
value: 29 45 68 89 90 34 17 j = −1
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 37 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 34
value: 29 45 68 89 90 34 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 38 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 34
value: 29 45 45 68 89 90 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 39 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 34
value: 29 34 45 68 89 90 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 40 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 17
value: 29 34 45 68 89 90 17
i
j
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 41 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 17
value: 29 29 34 45 68 89 90 j = −1
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 42 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 v = 17
value: 17 29 34 45 68 89 90 j = −1
i
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 43 / 106
Insertion sort (decrease-and-conquer)
Insertion sort
Idea: provided that A[0..n − i] is sorted, where 2 ≤ i ≤ n, to sort
A[0..n − i + 1], we “insert” A[n − i + 1] appropriately (dec. by a constant)
index: 0 1 2 3 4 5 6 i =7
value: 17 29 34 45 68 89 90
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 44 / 106
Insertion sort (decrease-and-conquer)
Pn−1 Pi−1
Cworst (n) = i=1 j=0 1
Pn−1
= i=1 ((i − 1) − 0 + 1)
Pn−1
= i=1 i
(1+(n−1))((n−1)−1+1)
= 2
n2 −n
= 2 ∈ Θ(n2 )
Pn−1
Cbest (n) = i=1 1 = n − 1 − 1 + 1 = n − 1 ∈ Θ(n)
n2
Cavg (n) ≈ 4 ∈ Θ(n2 )
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 45 / 106
Mergesort (divide-and-conquer)
Agenda
3 Mergesort (divide-and-conquer)
4 Bibliography
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 46 / 106
Mergesort (divide-and-conquer)
Divide-and-conquer
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 47 / 106
Mergesort (divide-and-conquer)
Mergesort1
1
Source: A. Levitin. Introduction to the Design and Analysis of Algorithms. 2011.
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 48 / 106
Mergesort (divide-and-conquer)
Mergesort
Algorithm: Mergesort(A[0..n-1], l, r)
1 if l < r then
2 m ← b(l + r )/2c;
3 Mergesort(A, l, m); Mergesort(A, m + 1, r ); Merge(A, l, r );
Algorithm: Merge(A[0..n-1], l, r)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c;
3 i1 ← l; i2 ← m + 1;
4 for curr ← l to r do
5 if i1 = m + 1 then A[curr ] ← temp[i2++];
6 else if i2 > r then A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 49 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
index: 0 1 2 3 4
5 Merge(A, l, r );
A: 5 2 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 50 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
index: 0 1 2 3 4
A: 5 2 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 51 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
( , 0, 0) ( , 1, 1)
index: 0 1 2 3 4
A: 5 2 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 52 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
( , 0, 0) ( , 1, 1)
index: 0 1 2 3 4
A: 5 2 1 7 0
l,r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 53 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
( , 0, 0) ( , 1, 1)
index: 0 1 2 3 4
A: 5 2 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 54 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
( , 0, 0) ( , 1, 1)
index: 0 1 2 3 4
A: 5 2 1 7 0
l,r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 55 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
index: 0 1 2 3 4
A: 5 2 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 56 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
5 if i1 = m + 1 then ( , 0, 1) ( , 2, 2)
A[curr ] ← temp[i2++]; index: 0 1 2 3 4
6 else if i2 > r then A: 5 2 1 7 0
A[curr ] ← temp[i1++]; l r
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++]; temp: 5 2
9 else A[curr ] ← temp[i2++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 57 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
5 if i1 = m + 1 then ( , 0, 1) ( , 2, 2)
A[curr ] ← temp[i2++]; index: 0 1 2 3 4
6 else if i2 > r then A: 5 2 1 7 0
A[curr ] ← temp[i1++]; l r
7 else if temp[i1] ≤ temp[i2] then c
8 A[curr ] ← temp[i1++]; temp: 5 2
9 else A[curr ] ← temp[i2++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 58 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
5 if i1 = m + 1 then ( , 0, 1) ( , 2, 2)
A[curr ] ← temp[i2++]; index: 0 1 2 3 4
6 else if i2 > r then A: 2 2 1 7 0
A[curr ] ← temp[i1++]; l r
7 else if temp[i1] ≤ temp[i2] then c
8 A[curr ] ← temp[i1++]; temp: 5 2
9 else A[curr ] ← temp[i2++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 59 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
5 if i1 = m + 1 then ( , 0, 1) ( , 2, 2)
A[curr ] ← temp[i2++]; index: 0 1 2 3 4
6 else if i2 > r then A: 2 2 1 7 0
A[curr ] ← temp[i1++]; l r
7 else if temp[i1] ≤ temp[i2] then c
8 A[curr ] ← temp[i1++]; temp: 5 2
9 else A[curr ] ← temp[i2++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 60 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
5 if i1 = m + 1 then ( , 0, 1) ( , 2, 2)
A[curr ] ← temp[i2++]; index: 0 1 2 3 4
6 else if i2 > r then A: 2 5 1 7 0
A[curr ] ← temp[i1++]; l r
7 else if temp[i1] ≤ temp[i2] then c
8 A[curr ] ← temp[i1++]; temp: 5 2
9 else A[curr ] ← temp[i2++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 61 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
5 if i1 = m + 1 then ( , 0, 1) ( , 2, 2)
A[curr ] ← temp[i2++]; index: 0 1 2 3 4
6 else if i2 > r then A: 2 5 1 7 0
A[curr ] ← temp[i1++]; l r
7 else if temp[i1] ≤ temp[i2] then c
8 A[curr ] ← temp[i1++]; temp: 5 2
9 else A[curr ] ← temp[i2++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 62 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
index: 0 1 2 3 4
A: 2 5 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 63 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 0, 1) ( , 2, 2)
index: 0 1 2 3 4
A: 2 5 1 7 0
l,r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 64 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
index: 0 1 2 3 4
5 Merge(A, l, r );
A: 2 5 1 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 65 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 2 5 1 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 66 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 2 5 1 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 67 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 5 1 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 68 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 5 1 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 69 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 1 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 70 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 1 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 71 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 72 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 2 5 1
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 73 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
index: 0 1 2 3 4
5 Merge(A, l, r );
A: 1 2 5 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 74 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 3, 3) ( , 4, 4)
index: 0 1 2 3 4
A: 1 2 5 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 75 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 3, 3) ( , 4, 4)
index: 0 1 2 3 4
A: 1 2 5 7 0
l,r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 76 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 3, 3) ( , 4, 4)
index: 0 1 2 3 4
A: 1 2 5 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 77 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r ); ( , 3, 3) ( , 4, 4)
index: 0 1 2 3 4
A: 1 2 5 7 0
l,r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 78 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m);
( , 0, 2) ( , 3, 4)
4 Mergesort(A, m + 1, r );
index: 0 1 2 3 4
5 Merge(A, l, r );
A: 1 2 5 7 0
l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 79 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 7 0
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 80 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 7 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
c
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 7 0
8 A[curr ] ← temp[i1++]; i1 i2
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 81 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 0 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
A[curr ] ← temp[i1++]; c
7 else if temp[i1] ≤ temp[i2] then temp: 7 0
8 A[curr ] ← temp[i1++]; i1
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 82 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 0 0
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
A[curr ] ← temp[i1++]; c
7 else if temp[i1] ≤ temp[i2] then temp: 7 0
8 A[curr ] ← temp[i1++]; i1
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 83 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 0 7
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
A[curr ] ← temp[i1++]; c
7 else if temp[i1] ≤ temp[i2] then temp: 7 0
8 A[curr ] ← temp[i1++]; i1
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 84 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1;
( , 0, 2) ( , 3, 4)
4 for curr ← l to r do
index: 0 1 2 3 4
5 if i1 = m + 1 then
A: 1 2 5 0 7
A[curr ] ← temp[i2++];
l r
6 else if i2 > r then
A[curr ] ← temp[i1++];
7 else if temp[i1] ≤ temp[i2] then temp: 7 0
8 A[curr ] ← temp[i1++]; i1
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 85 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; ( , 0, 4)
3 Mergesort(A, l, m); index: 0 1 2 3 4
4 Mergesort(A, m + 1, r ); A: 1 2 5 0 7
5 Merge(A, l, r ); l r
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 86 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 1 2 5 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++];
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 87 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 1 2 5 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 88 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 2 5 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 89 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 2 5 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 90 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 5 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 91 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 5 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 92 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 2 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 93 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 2 0 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 94 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 2 5 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 95 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 2 5 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1 i2
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++];
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 96 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 2 5 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++]; c
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++]; i2 = 5
9 else A[curr ] ← temp[i2++];
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 97 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Merge(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 for i ← l to r do temp[i] ← A[i];
2 m ← b(l + r )/2c; ( , 0, 4)
3 i1 ← l; i2 ← m + 1; index: 0 1 2 3 4
4 for curr ← l to r do A: 0 1 2 5 7
5 if i1 = m + 1 then l r
A[curr ] ← temp[i2++];
6 else if i2 > r then temp: 1 2 5 0 7
A[curr ] ← temp[i1++]; i1
7 else if temp[i1] ≤ temp[i2] then
8 A[curr ] ← temp[i1++]; i2 = 5
9 else A[curr ] ← temp[i2++]; c=5
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 98 / 106
Mergesort (divide-and-conquer)
Mergesort
mergesort(
Algorithm: Mergesort(A[0..n-1], l, r)
[5,2,1,7,0],0,4)
1 if l < r then
2 m ← b(l + r )/2c; index: 0 1 2 3 4
3 Mergesort(A, l, m); A: 0 1 2 5 7
4 Mergesort(A, m + 1, r );
5 Merge(A, l, r );
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 99 / 106
Mergesort (divide-and-conquer)
Divide-and-conquer
Complexity (in general terms):
C(n) = aC(n/b) + f (n), where a ≥ 1, b ≥ 1, and a ≤ b
n = size of the problem instance
b = number of sub-instances
a = number of sub-instances that need to be solved
f (n) = dividing and combining effort
Therefore
Cworst (n) = 2Cworst (n/2) + n − 1 for n > 1
Cworst (1) = 0 for n ≤ 1
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 101 / 106
Mergesort (divide-and-conquer)
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 102 / 106
Mergesort (divide-and-conquer)
2
Source: http://bigocheatsheet.com/
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 103 / 106
Bibliography
Agenda
3 Mergesort (divide-and-conquer)
4 Bibliography
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 104 / 106
Bibliography
Bibliography
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 105 / 106
SELECTION SORT | INSERTION SORT | MERGESORT
Gustavo Carvalho
([email protected])
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 106 / 106