2024-02-24 02:43:29 +00:00
|
|
|
---
|
|
|
|
title: Merge Sort
|
|
|
|
TARGET DECK: Obsidian::STEM
|
|
|
|
FILE TAGS: algorithm::sorting
|
|
|
|
tags:
|
|
|
|
- algorithm
|
|
|
|
- sorting
|
|
|
|
---
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
2024-03-22 14:10:08 +00:00
|
|
|
| Property | Value |
|
|
|
|
| ----------- | ----------------- |
|
|
|
|
| Best Case | $\Omega(n\lg{n})$ |
|
|
|
|
| Worst Case | $O(n\lg{n})$ |
|
|
|
|
| Avg. Case | $O(n\lg{n})$ |
|
|
|
|
| Aux. Memory | $O(n)$ |
|
|
|
|
| Stable | - |
|
|
|
|
| Adaptive | - |
|
2024-02-24 02:43:29 +00:00
|
|
|
|
|
|
|
![[merge-sort.gif]]
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What does the term "merge" in `MERGE_SORT` refer to?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: The primary operation used to combine array halves.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467101-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What is `MERGE_SORT`'s best case runtime?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: $\Omega(n\lg{n})$
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467109-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What is `MERGE_SORT`'s worst case runtime?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: $O(n\lg{n})$
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467112-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What is `MERGE_SORT`'s average case runtime?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: $O(n\lg{n})$
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467115-->
|
|
|
|
END%%
|
|
|
|
|
2024-03-22 14:10:08 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
How much auxiliary space does `MERGE_SORT` use?
|
|
|
|
Back: $O(n)$
|
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
|
|
|
<!--ID: 1711024614320-->
|
|
|
|
END%%
|
|
|
|
|
2024-02-24 02:43:29 +00:00
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What is the "divide" step of `MERGE_SORT`?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: Divide the input array into two subarrays of half size.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467118-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What is the "conquer" step of `MERGE_SORT`?
|
|
|
|
Back: Call `MERGE_SORT` on the "divide"-step's two subarrays.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467121-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What is the "combine" step of `MERGE_SORT`?
|
2024-03-01 14:12:43 +00:00
|
|
|
Back: Merge the sorted subarrays produced by the "conquer" step.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467125-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
When does `MERGE_SORT` "bottom out"?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: When the sequence to be sorted has length 1 or less.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467133-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
```c
|
|
|
|
static void merge(int i, int mid, int j, int *A) {
|
|
|
|
int si = mid - i + 1;
|
|
|
|
int sj = j - (mid + 1) + 1;
|
|
|
|
|
|
|
|
int *L = malloc(sizeof(int) * (si + 1));
|
|
|
|
int *R = malloc(sizeof(int) * (sj + 1));
|
|
|
|
|
|
|
|
L[si] = INT_MAX;
|
|
|
|
R[sj] = INT_MAX;
|
|
|
|
|
|
|
|
for (int k = 0; k < si; ++k) {
|
|
|
|
L[k] = A[i + k];
|
|
|
|
}
|
|
|
|
for (int k = 0; k < sj; ++k) {
|
|
|
|
R[k] = A[mid + 1 + k];
|
|
|
|
}
|
|
|
|
|
|
|
|
int topL = 0, topR = 0;
|
|
|
|
for (int k = 0; k < j - i + 1; ++k) {
|
|
|
|
if (L[topL] < R[topR]) {
|
|
|
|
A[i + k] = L[topL++];
|
|
|
|
} else {
|
|
|
|
A[i + k] = R[topR++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(L);
|
|
|
|
free(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge_sort(int i, int j, int *A) {
|
|
|
|
if (j <= i) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int mid = (i + j) / 2;
|
|
|
|
merge_sort(i, mid, A);
|
|
|
|
merge_sort(mid + 1, j, A);
|
|
|
|
merge(i, mid, j, A);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
Where in `MERGE_SORT`'s implementation are sentinels useful?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: As the last elements of the two arrays to combine.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467137-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
2024-03-06 20:25:34 +00:00
|
|
|
What sentinel values are typically used in `MERGE_SORT`'s "merge" operation?
|
2024-02-24 02:43:29 +00:00
|
|
|
Back: $\infty$ or the record type's equivalent.
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742467141-->
|
|
|
|
END%%
|
|
|
|
|
|
|
|
%%ANKI
|
|
|
|
Basic
|
|
|
|
What sorting algorithm does the following demonstrate?
|
|
|
|
![[merge-sort.gif]]
|
2024-03-06 20:25:34 +00:00
|
|
|
Back: `MERGE_SORT`
|
2024-03-19 00:28:34 +00:00
|
|
|
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|
2024-02-24 02:43:29 +00:00
|
|
|
<!--ID: 1708742590435-->
|
|
|
|
END%%
|
|
|
|
|
2024-03-22 15:26:41 +00:00
|
|
|
## Bibliography
|
2024-02-24 02:43:29 +00:00
|
|
|
|
2024-03-19 00:28:34 +00:00
|
|
|
* Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
|