Enhance Library Sort: key/reverse, gap rebalancing, docs#13392
Closed
leo-soumyajit wants to merge 1 commit intoTheAlgorithms:masterfrom
Closed
Enhance Library Sort: key/reverse, gap rebalancing, docs#13392leo-soumyajit wants to merge 1 commit intoTheAlgorithms:masterfrom
leo-soumyajit wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
Closing this pull request as invalid@leo-soumyajit, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines. If you're facing any problem on how to mark a checkbox, please read the following instructions:
NOTE: Only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title
Enhance: Add Library Sort (gapped insertion) with docs and examples
Summary
Adds an educational, well-documented implementation of Library Sort (gapped insertion) to the sorts directory, including key/reverse parameters, gap rebalancing, and a minimal example script for quick verification. This provides learners with a practical, annotated reference for a distribution-aware insertion strategy.
What’s included
New: sorts/library_sort.py implementing Library Sort with:
key and reverse parameters aligned with Python’s sorting API.
epsilon parameter to control gap density and rebalance behavior.
detailed docstrings and inline comments explaining the data structures (gapped arrays, logical positions) and the rebalance heuristic.
Example: main block demonstrating usage with a small dataset.
Complexity notes in the header docstring: average O(n log n) with high probability, worst O(n^2), space O(n(1+epsilon)).
Motivation
Teaches a lesser-known, research-backed variant of insertion sort that achieves expected O(n log n) by maintaining evenly distributed gaps and rebalancing when clusters form. Ideal for learners comparing practical sort designs beyond classic algorithms.
Implementation notes
Uses a sparse backing array for keys/values plus a “pos” list for logical order; insertions do a binary search on logical order and choose a midpoint slot, rebalancing when local density prevents gap placement.
API mirrors built-in sorted where practical, enabling side-by-side comparison in exercises and tests.
How to test
Run the built-in example:
python sorts/library_sort.py should print a sorted “After:” list.
Sanity checks (suggested):
Compare against sorted on random arrays and edge cases (empty, single element, duplicates).
Verify key and reverse, e.g., key=lambda x: x % 10 and reverse=True.
Scope and trade-offs
Focuses on pedagogy and clarity; not a drop-in replacement for Timsort in production scenarios. For arbitrary real-world data, Python’s built-in sort remains recommended due to stability and adaptiveness.
Library Sort worst case remains O(n^2); documentation calls this out to set correct expectations.
Related
Closes #13203 (advanced sorting algorithms request).
References: Bender et al., “Insertion Sort is O(n log n)” (Library Sort analysis); Wikipedia overview for algorithm characteristics.