A focused algorithm/data-structure optimization project based on the merge-sort implementation in keon/algorithms. The upstream repository's merge_sort.py was inspected for this exercise. This ZIP is a standalone, focused lab, not a complete copy of the upstream repository.
The reference implementation recursively copies list slices (values[:mid] and values[mid:]) and allocates intermediate lists at each recursive level. The optimized version:
- Recurses over index ranges and avoids recursive slice copies.
- Allocates one reusable scratch buffer for merging.
- Uses insertion sort on short runs to reduce recursive/merge overhead.
- Skips merging when the two sorted halves are already in order.
- Fast-paths fully ascending or non-increasing input in linear time.
- Accepts any iterable and does not mutate the caller's input list.
- Requires only
<from the sorted items, the same minimal protocol CPython's built-in sort relies on, so__lt__-only objects work here too.
Both implementations have O(n log n) average/worst-case time complexity and O(n) auxiliary storage. This project does not claim the pure-Python version will beat CPython's built-in sorted(); built-in sorting is included as an important real-world reference.
- Python 3.10 or newer
- No third-party packages required
From the project root:
python -m unittest discover -s tests -v python benchmarks/benchmark.py python benchmarks/benchmark.py --min-run-sweep python merge_sort_lab.py
The benchmark checks output correctness before and after every timed call. For a fair end-to-end comparison, the upstream-style baseline copies its input, the optimized function copies its input internally, and built-in sorted() returns a new list. It tests random, sorted, reverse-sorted, and nearly-sorted integer lists at sizes 1,000, 10,000, and 50,000, and reports the minimum wall time across repeated runs; the minimum is far more reproducible than a mean or median when the machine is busy. Use --repeats, --sizes, and --min-runs to change the workload, or --min-run-sweep to compare insertion-sort cutoffs on the same inputs. Results are machine- and Python-version-dependent; rerun locally before making performance claims.
merge_sort_lab.py reference and optimized implementations
tests/test_merge_sort.py edge cases, randomized tests, stability checks
benchmarks/benchmark.py reproducible benchmark and cutoff sweep
docs/OPTIMIZATION_NOTES.md design and trade-offs
BENCHMARK_RESULTS.md measured results from this environment
assets/ banner and GitHub social preview
README_CN.md Chinese overview and quick start
NOTICE.md upstream attribution and scope
LICENSE MIT license, upstream and this work
pyproject.toml packaging metadata
.github/workflows/ci.yml test matrix on push and pull request
Create an empty repository, extract this ZIP, then run:
git init git add . git commit -m "Optimize merge sort allocation and merge overhead" git branch -M main git remote add origin https://github.com/YOUR-NAME/merge-sort-performance-lab.git git push -u origin main
Replace YOUR-NAME with your GitHub account name. Do not claim this is an official change to the upstream project; it is an independent optimization study derived from its MIT-licensed example. A useful next step would be to open a pull request upstream only if the change is benchmark-supported and follows that repository's contribution guidelines.
