Skip to content

PantoYT/Sorting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ข Sorting Algorithms Collection

100 sorting algorithms. From O(1) to O(โˆž). From production-ready to multiverse-borrowing.

python benchmark.py                         # full benchmark
python benchmark.py -a BubbleSort,TimSort   # specific algorithms
python benchmark.py -f Quick --top 5        # filter + top 5
python benchmark.py --list                  # list all 100

python visualize.py                         # interactive menu
python visualize.py QuickSort               # single animation
python visualize.py Bubble,Merge,Quick      # compare side-by-side
python visualize.py --list                  # list visualizable algorithms

๐Ÿ“ Structure

Sorting/
โ”œโ”€โ”€ data.py              # test datasets
โ”œโ”€โ”€ benchmark.py         # auto-benchmark, zero hardcoding
โ”œโ”€โ”€ visualize.py         # matplotlib animations, comma-list compare
โ””โ”€โ”€ algorithms/
    โ”œโ”€โ”€ __init__.py      # auto-discovery
    โ””โ”€โ”€ *.py             # 100 algorithms, one per file

Adding a new algorithm = one new file. Everything else updates automatically.


๐Ÿ“Š All 100 Algorithms

โšก O(n log n) โ€” Production Ready

Algorithm Avg Worst Stable Notes
TimSort O(n log n) O(n log n) โœ“ Powers Python sorted()
MergeSort O(n log n) O(n log n) โœ“ Classic divide & conquer
IterativeMergeSort O(n log n) O(n log n) โœ“ No recursion, stack-safe
AdaptiveMergeSort O(n log k) O(n log n) โœ“ k = number of runs
WeaveMergeSort O(n log n) O(n log n) โœ“ Cache-friendly bottom-up
InPlaceMergeSort O(n logยฒn) O(n logยฒn) โœ“ O(1) extra memory
MergeSortParallel O(n log n) O(n log n) โœ“ Multi-threaded
QuickSort O(n log n) O(nยฒ) โœ— Fastest in practice
RandomizedQuickSort O(n log n) O(nยฒ) rare โœ— Random pivot
StableQuickSort O(n log n) O(nยฒ) โœ“ Extra memory for stability
TernarySplitQuickSort O(n log n) O(n log n) โœ— Dutch flag partition
DualPivotQuickSort O(n log n) O(n log n) โœ— Java Arrays.sort()
KnuthSort O(n log n) O(n log n) โœ— 3-way, Knuth vol.3
IntroSort O(n log n) O(n log n) โœ— C++ std::sort
HeapSort O(n log n) O(n log n) โœ— Guaranteed, in-place
SmoothSort O(n log n) O(n log n) โœ— Leonardo heap
ShellSort O(n logยฒn) O(n logยฒn) โœ— Surprisingly fast
GrailSort O(n log n) O(n log n) โœ“ O(1) memory
WikiSort O(n log n) O(n log n) โœ“ Block merge, O(1) memory
QuadSort O(n log n) O(n log n) โœ“ Sorting network for 4-blocks
BitonicSort O(n logยฒn) O(n logยฒn) โœ— GPU-friendly
BitonicMergeSort O(n logยฒn) O(n logยฒn) โœ— Explicit merge network
SortingNetwork O(n logยฒn) O(n logยฒn) โœ— Fixed comparator network
TreeSort O(n log n) O(nยฒ) โœ“ BST in-order
TournamentSort O(n log n) O(n log n) โœ— Tournament tree
PatienceSort O(n log n) O(n log n) โœ“ Patience solitaire
PatienceOptSort O(n log n) O(n log n) โœ“ Binary search piles
PatienceSortIterative O(n log n) O(n log n) โœ“ No recursion
PatienceMergeSort O(n log n) O(n log n) โœ“ Patience + k-way merge
DropMergeSort O(n log n) O(n log n) โœ“ O(n) on nearly sorted
StrandSort O(nยฒ) worst O(n) best โœ“ Extracts sorted runs
WeaveSort O(n log n) O(n log n) โœ“ Bottom-up weaving
CartesianSort O(n log n) O(nยฒ) โœ— Cartesian tree
FunnelSort O(n logยฒn) O(n logยฒn) โœ“ Cache-oblivious
SampleSort O(n log n) O(n log n) โœ“ Parallel-friendly
HybridSort O(n log n) O(n log n) โœ“ Adaptive: picks best algo
BlockSort O(n log n) O(n log n) โœ“ sqrt(n) blocks
LazySort O(n log n) O(n log n) โœ“ Defers sort until access
YogurtSort O(n log n) O(n log n) โœ“ Natural runs + merge

๐Ÿš€ Faster than O(n log n) โ€” Non-Comparison

Algorithm Complexity Constraint
CountingSort O(n + k) integers, range k
RadixSort O(nk) integers
BucketSort O(n + k) avg uniform distribution
PigeonHoleSort O(n + k) integers, range โ‰ˆ n
PigeonSort O(n + k) integers, pigeonhole variant
PigeonRaceSort O(n + k) satirical pigeonhole
BeadSort O(S) non-negative integers
GravitySort O(S) non-negative integers
SpreadSort O(n) avg Boost C++
FlashSort O(n) avg uniform distribution
PostmanSort O(nk) MSD Radix
AmericanFlagSort O(nk) MSD, in-place
ProxmapSort O(n) avg uniform distribution

๐ŸŒ O(nยฒ) โ€” Classic & Educational

Algorithm Stable Notes
BubbleSort โœ“ The famous one
ReverseBubbleSort โœ“ Scans right-to-left
AntiBubbleSort โœ— Reverses correctly ordered pairs
InsertionSort โœ“ Best on nearly-sorted
BinaryInsertionSort โœ“ Binary search for position
RecursiveInsertionSort โœ“ Recursive variant
SelectionSort โœ— Fewest writes
ExchangeSort โœ— Every element vs every other
CocktailShakerSort โœ“ Bidirectional bubble
CombSort โœ— Bubble with gap > 1
GnomeSort โœ“ Garden gnome algorithm
NaiveSort โœ— Restarts after every swap
OddEvenSort โœ“ GPU/parallel design
EvenOddTranspositionSort โœ“ SIMD variant
CycleSort โœ— Minimal writes
PancakeSort โœ— Prefix reversals only
BurntPancakeSort โœ— Two-sided pancakes
PancakeNumberSort โœ— Tracks flip sequence
PancakeSortOptimized โœ— 2*(n-1) flips max
LibrarySort โœ“ Insertion with gaps
ShuffleSort โœ— Fisher-Yates shuffle attempt
EfficientBogoSort โœ— "Optimized" BogoSort
UnstableSort โœ— Deliberately unstable
XorSort โœ— XOR swap trick
ZigZagSort โœ“ Zigzag weave

๐ŸŒฟ Nature-Inspired

Algorithm Notes
GeneticSort Evolutionary, crossover + mutation
AntSort Ant colony optimization
QuantumSort Simulates quantum parallelism
VinoSort Wine decanting metaphor ๐Ÿท
YogurtSort Fermentation/culture merging
JigsawSort Puzzle-piece block fitting

๐Ÿ’€ Satirical / Deliberately Bad

Algorithm Complexity Lore
QuantumBogoSort O(1)โ€  Borrows result from a sorted parallel universe
BogoSort O(n ยท n!) Random shuffle until sorted
BogoBogoSort O((n+1)!) n > 4 will outlive the sun
StalinSort O(n) Removes non-conforming elements
SlowSort O(n^(log n/log log n)) Deliberately slowest correct sort
StoogeSort O(n^2.7) "Multiply and surrender"
MiracleSort O(โˆž) Awaits cosmic radiation
SleepSort O(max(n)) Thread sleep = element value
GaslitSort O(1) (claimed) Denies having sorted anything
McCarthySort O(n! ยท n) Removes random elements recursively
CorruptionSort O(n log n) + bugs Sorts then corrupts 8%
AntiBubbleSort O(nยฒ) Unsorting machine
EscapeSort O(nยฒ) Elements try to escape position
OscarSort O(nยฒ) Complains the entire time
DrunkSort O(?) 70% correct, 30% chaos
ChatGPTSort_v1 O(n log n) + 10% hallucinations Context window, content policy
ChatGPTSort_v2 O(๐Ÿ’ธ/token) Real OpenAI API
ClaudeSort O(n log n) + questions Asks for context first

โ€  Time complexity: O(1) โ€” just an observation. The sorted universe already exists. We just needed to look.

๐Ÿ“Š Alphabet Coverage

Every letter Aโ€“Z has at least one algorithm:

A  B  C  D  E  F  G  H  I  J  K  L  M
โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“

N  O  P  Q  R  S  T  U  V  W  X  Y  Z
โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“  โœ“

All 26/26 covered. ๐ŸŽ‰


๐Ÿš€ Usage

from algorithms import merge_sort, quick_sort, tim_sort, REGISTRY

data = [64, 34, 25, 12, 22, 11, 90]
print(merge_sort(data))   # [11, 12, 22, 25, 34, 64, 90]

# iterate all algorithms
for name, func in REGISTRY.items():
    result = func(data[:])
# Benchmark examples
python benchmark.py                          # all 100
python benchmark.py -a "Merge,Quick,Tim"     # compare three
python benchmark.py -f Pancake --size 50     # filter + custom size
python benchmark.py --list                   # full list

# Visualizer examples
python visualize.py                          # menu
python visualize.py QuickSort --size 30      # single, 30 elements
python visualize.py Bubble,Merge,Quick       # compare 3 side-by-side
python visualize.py --list                   # list visualizable

โž• Adding Your Own Algorithm

  1. Create algorithms/XxxSort.py
  2. Implement xxx_sort(list) โ€” must return a new list, not modify in-place
  3. Done. benchmark.py, __init__.py detect it automatically.
# algorithms/XyzSort.py
from data import *

def xyz_sort(lista):
    lst = lista.copy()
    # your implementation
    return lst

if __name__ == "__main__":
    from data import losowa
    print(xyz_sort(losowa))

Add a visualization

# in your own file or directly in visualize.py
from visualize import register_algorithm

def _xyz_steps(lst):
    a = lst[:]
    # yield (state, compare_indices, sorted_indices, label)
    yield a[:], [0, 1], [], "Comparing"
    yield a[:], [], list(range(len(a))), "Done!"

register_algorithm("XYZ Sort", _xyz_steps)

๐Ÿค Pull Requests

Simple rules:

  • One file = one algorithm (XxxSort.py)
  • Return a new list (lst = lista.copy() at the start)
  • Top comment: complexity + how it works
  • Must pass: assert xxx_sort([3,1,2]) == [1,2,3]
  • Satirical algorithms welcome (see QuantumBogoSort.py, GaslitSort.py)

Ideas:

  • ๐Ÿงฌ More evolutionary algorithms (Particle Swarm, Simulated Annealing)
  • ๐ŸŒ LocaleSort โ€” locale-aware string sorting
  • ๐Ÿ”ข More radix variants (MSD, ternary)
  • ๐ŸŽฎ Any algorithm with a good story

Every algorithm you add lives here alongside QuantumBogoSort. That's an honor.


๐Ÿ“ˆ Fun Facts

Fastest (theory):    QuantumBogoSort  โ€” O(1) in the surviving universe
Fastest (practice):  SpreadSort       โ€” O(n) average
Slowest (correct):   SlowSort         โ€” O(n^(log n / log log n))
Slowest (overall):   MiracleSort      โ€” O(โˆž)
Most honest:         StalinSort       โ€” always O(n), always "sorted"
Most philosophical:  QuantumBogoSort  โ€” the sorted list already exists, somewhere
Most paranoid:       GaslitSort       โ€” denies everything
Most Java:           DualPivotQuick   โ€” literally what Java uses
Most Python:         TimSort          โ€” literally what Python uses
Most pigeon:         PigeonRaceSort   โ€” n pigeons, simultaneously airborne

"Every sorting algorithm is correct if you define 'sorted' appropriately."
โ€” StalinSort Documentation, 2024

About

The last place any unsorted list wants to end up

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages