The Algorithms - Python
View on GitHubEducational reference implementations of every classical algorithm in Python. Type-hinted, doctested, MIT-licensed. About 1,381 algorithms across 43 categories.
About
TheAlgorithms/Python is a community-maintained reference of about 1,381 classical algorithm implementations in Python, organized into 43 top-level category directories under one repository. The project is MIT-licensed and sits near 220,000 GitHub stars at the pinned commit, with more than 1,900 contributors since the first commit in August 2016. The README disclaimer is one sentence and is not buried: "Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library." That sentence defines the product: a reading corpus, not a runtime library.
Every file that merges must pass three automated gates. ruff enforces about forty rule groups covering style, naming, and security. mypy validates type-hint coverage on every parameter and return value. pytest --doctest-modules executes every >>> example in every module docstring as a real test. The doctest convention is load-bearing: the example a reader sees in a function's docstring is one the contributor proved correct before the PR merged. CONTRIBUTING.md also mandates a sourced docstring linking to the originating paper or Wikipedia article, and doctests for both valid and invalid inputs, including one that proves the function raises ValueError on bad input.
The 43 categories span the standard algorithms-textbook curriculum plus a few extras: sorting, searching, graphs, dynamic programming, divide-and-conquer, greedy methods, backtracking, ciphers, hashes, data structures, strings, matrix and linear algebra, machine learning, neural networks, bit manipulation, geometry, conversions, physics, Project Euler solutions, and more. Each directory is independent. Adding a new algorithm means dropping one .py file into the right category directory; the CI run then regenerates DIRECTORY.md automatically. Contributors are told not to create new top-level directories, which keeps the catalog from fragmenting.
What this repo is not: a production library, a benchmarking project, or a PyPI package. There is no installable distribution, no shared module between files, no common utils.py. Two implementations of the same algorithm in different categories will diverge in style because they were merged years apart by different contributors working against the same checklist. The deliberate redundancy is what makes a single file readable in isolation: open one file and you have the entire algorithm, end to end.
The navigation tour gives a category-by-category map of the repository, sampling one canonical file from each major directory. The first-contribution tour walks the full contribution gate from README disclaimer to local ruff and pytest commands, so a reader planning to submit a PR can follow the entire path before opening one.
Architecture
The repository root holds a thin layer of metadata (README.md, CONTRIBUTING.md, DIRECTORY.md, LICENSE.md, pyproject.toml) and 48 top-level directories. Forty-three of those directories hold algorithms, each named after a category in snake_case: sorts, searches, graphs, dynamic_programming, data_structures, ciphers, hashes, maths, strings, matrix, machine_learning, and so on. The remaining five (.devcontainer, .github, .vscode, docs, scripts) are tooling. scripts/build_directory_md.py regenerates DIRECTORY.md after every successful CI run, which is why the contribution guide tells you not to hand-edit it.
Inside a category directory, every file is one algorithm. sorts/quick_sort.py is one self-contained 52-line file; searches/binary_search.py is a 434-line collection of binary-search variants. There are no shared modules between leaves, no utils.py, no plugin registry. If two algorithms need the same helper, the helper gets copied. This deliberate redundancy is what makes a single file readable in isolation: open it and you have everything.
There is no runtime. The repository ships no main, no service, no installable package on PyPI. pyproject.toml declares requires-python = ">=3.14", lists scientific Python dependencies (numpy, scipy, scikit-learn, pandas, matplotlib) for the algorithms that use them, and configures pytest --doctest-modules --showlocals as the only entry into the code. CI runs that pytest invocation on every push, with about ten files excluded because they need network access or heavy ML weights. The default way to use any algorithm is to copy the file into your own project and adapt it.
Start here
TheAlgorithms/Python holds about 1,381 algorithm implementations across 43 categories. All 52 tours in this collection fall into one of three tiers. The two intro tours orient you to the codebase; the 30 marquee tours go deep on one algorithm each, covering its history and Python implementation; the 20 category tours walk 4-7 representative algorithms per category for breadth. Start with one intro, then pick a marquee tour for depth or a category tour for breadth.
Intro tours
- First Contribution — walk the CONTRIBUTING.md checklist, the canonical
insertion_sort.pyfile shape, and thepytest --doctest-modulesgate before opening a PR. - Navigation — sample one file from each major category (sorts, searches, graphs, DP, ciphers) and land on the
autoapi_dirslist that names all 43.
Marquee algorithm tours
Sorts
- Bubble Sort — adjacent swaps, O(n squared) worst case, O(n) best case
- Insertion Sort — shift-and-insert loop, Comparable Protocol, six doctests
- Selection Sort — find minimum, swap to front, scan the remainder
- Merge Sort — divide in half, merge sorted halves, O(n log n) guaranteed
- Quicksort — random pivot, two-pass partition, list-spread recursive return
- Heap Sort — build a max-heap, extract-max loop, in-place O(n log n)
- Radix Sort — digit-by-digit counting sort, LSD order, no comparisons
- Counting Sort — frequency array, prefix-sum accumulation, O(n + k)
Searches
- Linear Search — one pass, index on hit, -1 on miss, no ordering required
- Binary Search — halving loop, pairwise sort guard, -1 on miss
- Jump Search — block jumps then linear scan, O(sqrt(n)) average
Graphs
- Breadth-First Search — queue, visited set, adjacency dict, doctest with cycle
- Depth-First Search — stack or recursion, visited set, pre-order traversal
- Dijkstra — priority queue, relaxation loop, non-negative weights only
- A* — admissible heuristic, f = g + h, open set as a min-heap
- Bellman-Ford — edge relaxation for n-1 rounds, negative-cycle detection
- Kruskal MST — sort edges by weight, union-find, greedy spanning tree
Dynamic programming
- Fibonacci DP — class-based memoization, walrus assignment, incremental extension
- 0/1 Knapsack — 2D DP table, include-or-skip recurrence, item reconstruction
- Longest Common Subsequence — 2D matrix fill, character-match branch, traceback
- Edit Distance — Levenshtein recurrence, three-way min, empty-string base cases
Cryptography and hashing
- Caesar Cipher — alphabet shift modulo length, encrypt/decrypt pair
- RSA Cipher — key generation, modular exponentiation, public/private pair
- SHA-1 — 512-bit blocks, 80 rounds, five 32-bit state words
- MD5 — four-round mixing, non-linear functions, 128-bit digest
Strings
- KMP String Match — failure function, O(n + m), no backtracking in the text
- Rabin-Karp — rolling hash, sliding window, O(n + m) average
Math
- Sieve of Eratosthenes — boolean array, marking multiples, O(n log log n)
Data structures
- Bloom Filter — k hash functions, bit array, probabilistic membership with no false negatives
- LRU Cache — doubly-linked list plus dict, O(1) get and put, capacity eviction
Category tours
- Sorts — bubble, insertion, selection, merge, quicksort, heap, radix: seven sorting algorithms side by side
- Searches — linear, binary, jump, interpolation, exponential, quick-select, ternary
- Ciphers — Caesar, Vigenere, RSA, Diffie-Hellman, Playfair, Hill, Enigma
- Hashes — MD5, SHA-1, SHA-256, Adler-32, Luhn, DJB2
- Data Structures — linked list, BST, AVL tree, heap, hash table, disjoint set, trie
- Conversions — decimal-to-binary, decimal-to-hex, binary-to-decimal, Roman numerals, temperature, RGB-to-HSV, length
- Bit Manipulation — popcount, Brian Kernighan, single-bit ops, reverse bits, find unique, power-of-two, Gray code
- Strings — KMP, Rabin-Karp, Boyer-Moore, Manacher, Z-function, Levenshtein, Aho-Corasick
- Maths — sieve, prime check, GCD, Fibonacci, Euclidean distance, binary exponentiation, CRT
- Geometry — geometry primitives, Graham scan, Jarvis march convex hull
- Matrix — matrix operations, rotate, spiral traversal, sorted search, inverse, Pascal triangle
- Graphs — BFS, DFS, Dijkstra, Bellman-Ford, A*, Kruskal, Prim across the 64-file graphs/ directory
- Backtracking — N-Queens, Sudoku, Knight Tour, Rat-in-Maze, permutations, word search, Hamiltonian cycle
- Greedy Methods — fractional knapsack, coin change, stock buy-sell, gas station, optimal merge
- Divide and Conquer — merge sort, convex hull, closest pair, Strassen, max subarray, power
- Dynamic Programming — Fibonacci, knapsack, LCS, edit distance, matrix chain, LIS, Floyd-Warshall
- Linear Algebra — Gaussian elimination, LU, Jacobi, matrix inverse, conjugate gradient, power iteration, rank
- Machine Learning — linear regression, logistic regression, k-means, kNN, decision tree, gradient descent, SVM
- Neural Networks — simple NN, backpropagation, two-hidden-layer, ReLU, Swish
- Project Euler — Problems 1-7, each with the cleanest solution file in the problem directory
Tours
Getting Started with Your First Contribution to TheAlgorithms/Python
beginnerWalk the README disclaimer, the CONTRIBUTING.md algorithm requirements, the canonical insertion_sort file shape, the auto-generated DIRECTORY.md, the pytest --doctest-modules gate, and the path to deeper algorithm tours.
Navigating TheAlgorithms/Python: A Tour of the 43 Categories
beginnerSample one canonical algorithm from each major category (a sort, a search, a graph traversal, a DP class, a cipher) and end on the autoapi_dirs list that names every category.
How Bubble Sort Works
intermediateCompare adjacent elements, swap if out of order, repeat. The algorithm that appears in every first-year course because the swapping motion is easy to picture.
How Insertion Sort Works
intermediatePick the next element, shift everything larger one position right, drop the element into the gap. The algorithm that outperforms quicksort on small runs.
How Selection Sort Works
intermediateFind the minimum of the unsorted region, swap it to the front, repeat. O(n squared) always, but only O(n) swaps total.
How Mergesort Works
intermediateSplit the list in half, sort each half recursively, merge the two sorted halves. The first algorithm written for a stored-program computer.
How Quicksort Works
intermediateA random pivot, a partition into lesser and greater, two recursive calls. The 1959 algorithm that still drives most stdlib sorts.
How Heapsort Works
intermediateBuild a max-heap over the array, then repeatedly swap the root to the back and restore the heap. O(n log n) worst case with no extra memory.
How Radix Sort Works
intermediateSort by least significant digit, then next digit, repeat. Non-comparison sort that runs in O(d times n) rather than O(n log n).
How Counting Sort Works
intermediateNo comparisons, no pivots. Count key frequencies, prefix-sum them, then place each element at its exact output index in one backward pass.
How Linear Search Works
intermediateScan every element in order, return the index on match, return -1 on miss. The lower bound for searching unsorted data.
How Binary Search Works
intermediateHalve the search space on every step. The 1946 algorithm that still trips up professionals on overflow and off-by-one errors.
How Jump Search Works
intermediateJump through sorted data in square-root-sized blocks, then linear-search the candidate block. O(sqrt(n)) with no recursion, no pivots.
How BFS Works
intermediateVisit every neighbor before going deeper. The FIFO queue invariant that guarantees shortest-path distances on unweighted graphs.
How DFS Works
intermediateFollow a path as deep as it goes before backtracking. The iterative stack-based implementation that mirrors the recursive structure without blowing the call stack.
How Dijkstra's Algorithm Works
intermediateA min-heap of (cost, node) tuples, a visited set, and a single relaxation loop. The 1956 algorithm at the core of OSPF and IS-IS routing.
How A* Search Works
intermediateCombine actual path cost with a heuristic estimate of remaining distance. The 1968 algorithm built for a robot, now in every map and game engine.
How Bellman-Ford Works
intermediateRelax every edge V-1 times, then run one more pass to detect negative cycles. The algorithm Dijkstra cannot replace.
How Kruskal's Algorithm Works
intermediateSort edges by weight, then add each one unless it creates a cycle. Union-find with path compression in 46 lines.
How Fibonacci DP Works
intermediateCache the sequence on a class instance, extend it on demand, return a slice. The canonical example of memoization replacing exponential recursion.
How the 0/1 Knapsack DP Works
intermediateFill a capacity-by-item table bottom-up, then trace back through it to recover which items go in the bag.
How Longest Common Subsequence Works
intermediateA 2-D DP table, one recurrence, and a traceback walk. The algorithm at the core of diff, git-merge, and DNA sequence alignment.
How Edit Distance Works
intermediateCount the minimum insertions, deletions, and substitutions to transform one string into another. The 1965 algorithm behind spell-check, fuzzy search, and DNA alignment.
How the Sieve of Eratosthenes Works
intermediateMark every multiple of each found prime starting at its square. What remains is a list of primes, produced in O(n log log n).
How the Caesar Cipher Works
intermediateA modular shift over an alphabet, mirrored to decrypt, exhausted to brute-force. The oldest attested cipher in the Western record.
How RSA Encryption Works
intermediateSplit the message into fixed-size integer blocks, raise each block to a modular power, and undo it with the private exponent.
How SHA-1 Works
intermediatePad the message, split into 512-bit blocks, expand each block to 80 words, run 80 rounds of bit operations, accumulate five 32-bit words into a 160-bit digest.
How MD5 Works
intermediateFour 32-bit state words, 64 rounds, a sin-derived constant table. The 1991 algorithm that still shows up in checksums everywhere.
How the Knuth-Morris-Pratt Algorithm Works
intermediatePreprocess the pattern into a failure array, then scan the text without backtracking. The 1977 algorithm that makes grep and DNA search O(n+m).
How Rabin-Karp String Matching Works
intermediateHash the pattern, slide a rolling hash across the text, confirm on match. The 1987 algorithm that makes multi-pattern search practical.
How a Bloom Filter Works
intermediateA bitarray, two hash functions, and a membership check that never gives false-negatives. Burton Bloom's 1970 data structure still in production databases.
How an LRU Cache Works
intermediateA doubly-linked list plus a hashmap gives O(1) get and put. The eviction policy at the core of CPU caches, browser caches, and functools.lru_cache.
Sorting Algorithms in TheAlgorithms/Python
beginnerSeven sorting strategies side by side: from the classroom bubble loop to radix buckets.
Search Algorithms in TheAlgorithms/Python
beginnerSeven search strategies from a simple scan to partition-based selection, each with a different trade-off.
Cipher Algorithms in TheAlgorithms/Python
intermediateSeven ciphers from ancient Rome to Diffie-Hellman: each a different answer to the question of what makes a message unreadable.
Hash Functions in TheAlgorithms/Python
beginnerSix hash algorithms from a 13-byte checksum to 256-bit cryptographic digests: each trading reliability for speed differently.
Data Structures in TheAlgorithms/Python
beginnerSeven fundamental data structures side by side: the pointer chain, the key-ordered tree, the self-balancing tree, the heap, the hash table, the union-find, and the prefix tree.
Conversion Algorithms in TheAlgorithms/Python
beginnerSeven base, numeral, and unit conversions showing how repeated division, recursion, lookup tables, and pivot factors each solve the same family of problems.
Bit Manipulation in TheAlgorithms/Python
beginnerSeven techniques that use bitwise operations to count, test, flip, reverse, and encode integers without arithmetic.
String Algorithms in TheAlgorithms/Python
intermediateSeven string search and comparison algorithms from KMP failure arrays to Aho-Corasick automata.
Maths Algorithms in TheAlgorithms/Python
intermediateSeven mathematical algorithms from prime sieves to the Chinese Remainder Theorem.
Geometry Algorithms in TheAlgorithms/Python
intermediateFour stops from primitive shapes to convex hull algorithms, with cross-product orientation as the shared computational primitive.
Matrix Algorithms in TheAlgorithms/Python
intermediateSix matrix operations from elementwise addition to Pascal's triangle generation.
Graph Algorithms in TheAlgorithms/Python
intermediateSeven graph traversal and shortest-path strategies from BFS queues to Prim's greedy expansion.
Backtracking Algorithms in TheAlgorithms/Python
intermediateSeven constraint-satisfaction problems solved by the same pattern: try a placement, recurse, and undo it when the branch fails.
Greedy Algorithms in TheAlgorithms/Python
intermediateFive problems where the locally optimal choice at each step produces the globally optimal result.
Divide and Conquer Algorithms in TheAlgorithms/Python
intermediateSix algorithms that split a problem in half, solve each half independently, and combine the results: from sorting to matrix multiplication to geometry.
Dynamic Programming in TheAlgorithms/Python
intermediateSeven DP patterns from memoized sequences to all-pairs shortest paths.
Linear Algebra Algorithms in TheAlgorithms/Python
intermediateSeven direct solvers and iterative methods, from Gaussian elimination to conjugate gradient, each exposing a different trade-off in numerical linear algebra.
Machine Learning Algorithms in TheAlgorithms/Python
intermediateSeven classical ML algorithms from linear models to kernel machines, each implemented from scratch in plain NumPy.
Neural Networks in TheAlgorithms/Python
intermediateFive files tracing how a network learns: from a single-neuron forward pass to backpropagation, multi-layer weight matrices, and the activation functions that make it possible.
Project Euler Solutions in TheAlgorithms/Python
intermediateThe first seven Project Euler problems, each solved in a single Python function with doctests, showing how mathematical insight reduces brute force to a handful of lines.
Origin Story
TheAlgorithms/Python: A Living Textbook of Classical AlgorithmsHow a community of 1,900 contributors keeps about 1,381 algorithm files type-hinted, doctested, and readable end to end.
Read the full storyRelated Projects
Create code tours for your project
Intraview lets AI create interactive walkthroughs of any codebase. Install the free VS Code extension and generate your first tour in minutes.
Install Intraview Free