mirror of
https://gitlab.com/magicalsoup/Highschool.git
synced 2025-01-23 16:11:46 -05:00
Update Merge Sort.md
This commit is contained in:
parent
057498ebc0
commit
88bf506222
@ -23,3 +23,44 @@ def mergesort(left, right, array[]):
|
||||
- Latop computer can run close to $`10^8`$ per second.
|
||||
- Supercomputer can execute $`10^{12}`$ compares per second.
|
||||
|
||||
### Insertion Sort $`(N^2)`$
|
||||
|computer|thousand $`(10^3)`$|million $`(10^6)`$|billion $`(10^9)`$|
|
||||
|:-------|:-------|:-------|:-------|
|
||||
|home|instant|2.8 hours|317 years|
|
||||
|super|instant|1 second|1 week|
|
||||
|
||||
### Merge Sort $`(N \log N)`$
|
||||
|
||||
|computer|thousand $`(10^3)`$|million $`(10^6)`$|billion $`(10^9)`$|
|
||||
|:-------|:-------|:-------|:-------|
|
||||
|home|instant|1 second|18 min|
|
||||
|super|instant|instant|instant|
|
||||
|
||||
|
||||
## Proof Sketch
|
||||
|
||||
**Proposition** Merge sort uses $`\le N \log N`$ compares to sort an array of length $`N`$.
|
||||
|
||||
**Proof Sketch** The number of compares $`C(N)`$ to mergesort an array of length $`N`$ satisfies the recurrence:
|
||||
|
||||
```math
|
||||
C(N) \le C(\lceil N/2 \rceil) + C(\lfloor N/2 \rfloor) + N \text{ for } N \gt 1, \text{ with } C(1) = 0.
|
||||
```
|
||||
|
||||
## Divide and conquer recurrence induction proof
|
||||
|
||||
**Proposition**. If $`D(N)`$ satisfies $`D(N)=2D(N/2) + N`$ for $`N \gt 1`$, with $`D(1) = 0,`$ then $`D(N) = N \log N`$.
|
||||
|
||||
**Proof** [assuming $`N`$ is a power of $`2`$]
|
||||
- Base case: $`N = 1`$
|
||||
- Inductive hypothesis: $`D(N) = N \log N`$.
|
||||
- Goal: show that $`D(2N) = (2N) = \log (2N)`$
|
||||
|
||||
$`D(2N) = 2D(N) + 2N`$ <br>
|
||||
$`\quad = 2 N \log N + 2N`$ <br>
|
||||
$`\quad = 2 N (log (2N) - 1) + 2N`$ <br>
|
||||
$`\quad = 2 N \log (2N)`$
|
||||
|
||||
|
||||
## Resources
|
||||
- Princeton University: https://algs4.cs.princeton.edu/lectures/22Mergesort.pdf
|
||||
|
Loading…
Reference in New Issue
Block a user