0% found this document useful (0 votes)
10 views106 pages

03 Mergesort

Uploaded by

Lucas Haas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views106 pages

03 Mergesort

Uploaded by

Lucas Haas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

SELECTION SORT | INSERTION SORT | MERGESORT

Gustavo Carvalho
([email protected])

Universidade Federal de Pernambuco


Centro de Informática, 50740-560, Brazil

Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 1 / 106
Selection sort (brute force)

Agenda

1 Selection sort (brute force)

2 Insertion sort (decrease-and-conquer)

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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

Algorithm: SelectionSort(A[0..n - 1])


1 for i ← 0 to n − 2 do
2 min ← i;
3 for j ← i + 1 to n − 1 do
4 if A[j] < A[min] then min ← j;
5 swap A[i] and A[min];

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)

Selection sort: complexity (basic op. = A[j] < A[min])

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

1 Selection sort (brute force)

2 Insertion sort (decrease-and-conquer)

3 Mergesort (divide-and-conquer)

4 Bibliography

Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 23 / 106
Insertion sort (decrease-and-conquer)

Brute force vs. Decrease-and-conquer


Example: computing an
Brute force (Θ(n)): |a ∗ {z
... ∗ a}
n

Decrease by a constant (Θ(n))


(
f (n − 1) · a if n > 0
f (n) =
1 if n = 0
Decrease by a constant factor (Θ(log n))

n/2 2 if n is even and positive
(a )

n (n−1)/2 2
a = (a ) · a if n is odd

1 if n = 0

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

Decrease by a constant factor


Other example: binary search

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Algorithm: InsertionSort(A[0..n - 1])


1 for i ← 1 to n − 1 do
2 v ← A[i]; j ← i − 1;
3 while j ≥ 0 ∧ A[j] > v do
4 A[j + 1] ← A[j]; j ← j − 1; // partial shift right
5 A[j + 1] ← v ;

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)

Insertion sort: complexity (basic op.: A[j] > v )

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

1 Selection sort (brute force)

2 Insertion sort (decrease-and-conquer)

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

1 A problem is divided into several subproblems


of the same type (ideally of about equal size)

2 The subproblems are solved

3 If necessary, the solutions to the subproblems are combined

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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[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

9 else A[curr ] ← temp[i2++]; i2 = 5

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

9 else A[curr ] ← temp[i2++]; i2 = 5

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

9 else A[curr ] ← temp[i2++]; i2 = 5

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

9 else A[curr ] ← temp[i2++]; c=5


i2 = 5

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

Master theorem: if f (n) ∈ Θ(nd ) holds, where d ≥ 0, then:



d if a < bd
Θ(n )

C(n) = Θ(nd log n) if a = bd

Θ(nlogb a ) if a > bd

The same applies for O and Ω


Decrease by a constant factor: a = 1
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 100 / 106
Mergesort (divide-and-conquer)

Mergesort: complexity (basic op. = comp. of keys)


Assuming n = 2k
C(n) = 2C(n/2) + Cmerge (n) for n > 1
C(1) = 0 for n ≤ 1

Worst case: when it is necessary to interleave both portions of A

Dividing and combining effort: Cmerge (n) = (n − 1)


Basic operation: number of key comparisons

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)

Mergesort: complexity (basic op. = comp. of keys)


Considering the master theorem
Cworst (n) = 2Cworst (n/2) + n − 1 for n > 1, thus a = b = 2
f (n) = n − 1 ∈ Θ(n) = Θ(nd ), thus d = 1
Since a = bd (i.e., 2 = 21 ), Cworst (n) ∈ Θ(nd log n) = Θ(n log n)

Pros and cons


⇑: stability
⇓: space efficiency

Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 102 / 106
Mergesort (divide-and-conquer)

Complexity of sorting algorithms2

2
Source: http://bigocheatsheet.com/
Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 103 / 106
Bibliography

Agenda

1 Selection sort (brute force)

2 Insertion sort (decrease-and-conquer)

3 Mergesort (divide-and-conquer)

4 Bibliography

Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 104 / 106
Bibliography

Bibliography

Chapter 3 (pp. 98–102) Chapter 7 (pp. 231–235,237–239,


Chapter 4 (pp. 131–136) 241–244) Clifford Shaffer.
Chapter 5 (pp. 169–175) Data Structures and
Anany Levitin. Algorithm Analysis.
Introduction to the Design and Dover, 2013.
Analysis of Algorithms.
3rd edition. Pearson. 2011.

Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 105 / 106
SELECTION SORT | INSERTION SORT | MERGESORT

Gustavo Carvalho
([email protected])

Universidade Federal de Pernambuco


Centro de Informática, 50740-560, Brazil

Gustavo Carvalho IF672 – Algorithms and Data Structures 24th June, 2022 106 / 106

You might also like