|
| 1 | +# Merge Sort |
| 2 | + |
| 3 | +In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. |
| 4 | +Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. |
| 5 | +Merge sort is a divide and conquer algorithm that was invented by John von Neumann in 1945. |
| 6 | +A detailed description and analysis of bottom-up mergesort appeared in a report by Goldstine and von Neumann as early as 1948. |
| 7 | + |
| 8 | +Conceptually, a merge sort works as follows: |
| 9 | + 1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). |
| 10 | + 2. Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list. |
| 11 | + |
| 12 | +{% method %} |
| 13 | +{% sample lang="py" %} |
| 14 | +[import:1-40, lang:"python"](code/python/merge.py) |
| 15 | +{% endmethod %} |
| 16 | + |
| 17 | +This algorithim can be made faster by doing the merge operations in parallel. |
| 18 | + |
| 19 | +<script> |
| 20 | +MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
| 21 | +</script> |
| 22 | +$$ |
| 23 | +\newcommand{\d}{\mathrm{d}} |
| 24 | +\newcommand{\bff}{\boldsymbol{f}} |
| 25 | +\newcommand{\bfg}{\boldsymbol{g}} |
| 26 | +\newcommand{\bfp}{\boldsymbol{p}} |
| 27 | +\newcommand{\bfq}{\boldsymbol{q}} |
| 28 | +\newcommand{\bfx}{\boldsymbol{x}} |
| 29 | +\newcommand{\bfu}{\boldsymbol{u}} |
| 30 | +\newcommand{\bfv}{\boldsymbol{v}} |
| 31 | +\newcommand{\bfA}{\boldsymbol{A}} |
| 32 | +\newcommand{\bfB}{\boldsymbol{B}} |
| 33 | +\newcommand{\bfC}{\boldsymbol{C}} |
| 34 | +\newcommand{\bfM}{\boldsymbol{M}} |
| 35 | +\newcommand{\bfJ}{\boldsymbol{J}} |
| 36 | +\newcommand{\bfR}{\boldsymbol{R}} |
| 37 | +\newcommand{\bfT}{\boldsymbol{T}} |
| 38 | +\newcommand{\bfomega}{\boldsymbol{\omega}} |
| 39 | +\newcommand{\bftau}{\boldsymbol{\tau}} |
| 40 | +$$ |
0 commit comments