GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - linluozi/merge-sort-performance-lab: An allocation-conscious, stable merge sort in pure Python, with a reproducible benchmark harness and an insertion-sort cutoff sweep.

BranchesTags
Open more actions menu

Latest commit

 

History

1 Commit
1 Commit

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
assets
assets
 
 
benchmarks
benchmarks
 
 
docs
docs
 
 
tests
tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
BENCHMARK_RESULTS.md
BENCHMARK_RESULTS.md
 
 
LICENSE
LICENSE
 
 
NOTICE.md
NOTICE.md
 
 
README.md
README.md
 
 
README_CN.md
README_CN.md
 
 
merge_sort_lab.py
merge_sort_lab.py
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

Merge Sort Performance Lab

Merge Sort Performance Lab — three rows of bars climbing from an unsorted input row, through two sorted runs, to one fully merged result

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.

What changed

The reference implementation recursively copies list slices (values[:mid] and values[mid:]) and allocates intermediate lists at each recursive level. The optimized version:

  1. Recurses over index ranges and avoids recursive slice copies.
  2. Allocates one reusable scratch buffer for merging.
  3. Uses insertion sort on short runs to reduce recursive/merge overhead.
  4. Skips merging when the two sorted halves are already in order.
  5. Fast-paths fully ascending or non-increasing input in linear time.
  6. Accepts any iterable and does not mutate the caller's input list.
  7. 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.

Requirements

  • Python 3.10 or newer
  • No third-party packages required

Run it

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.

Project layout

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

Publish to GitHub

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.

About

An allocation-conscious, stable merge sort in pure Python, with a reproducible benchmark harness and an insertion-sort cutoff sweep.

Topics

algorithm-optimizationalgorithmsbenchmarkmerge-sortperformancepythonsorting-algorithmsstable-sort

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.