Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion.
- About
- Features
- Getting Started
- Algorithm Categories
- Community Channels
- Contributing
- License
- List of Algorithms
This repository contains Python implementations of various algorithms and data structures for educational purposes. Whether you're a student learning algorithms, a developer preparing for technical interviews, or someone interested in computer science fundamentals, this collection provides clear and well-documented examples.
This repository includes implementations of algorithms across multiple categories:
- Sorting Algorithms: Bubble sort, Quick sort, Merge sort, Heap sort, and more
- Searching Algorithms: Binary search, Linear search, Jump search, Interpolation search
- Data Structures: Linked lists, Stacks, Queues, Trees, Graphs, Hash tables
- Graph Algorithms: BFS, DFS, Dijkstra's algorithm, Floyd-Warshall, Bellman-Ford
- Dynamic Programming: Knapsack, Longest Common Subsequence, Edit Distance
- Machine Learning: Neural networks, Linear regression, K-means clustering
- Mathematical Algorithms: Prime number algorithms, GCD, LCM, Number theory
- String Algorithms: Pattern matching, String manipulation, Parsing
- Cryptography: Various cipher implementations
- Computer Vision: Image processing algorithms
- And many more!
All implementations include:
- Clear documentation and explanations
- Type hints for better code readability
- Doctests for validation
- Educational comments
-
Clone the repository
git clone https://github.com/TheAlgorithms/Python.git cd Python -
Set up a virtual environment (recommended)
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
Each algorithm is self-contained in its own file. You can run any algorithm directly or import it into your own projects.
Example 1: Running an algorithm directly
python sorts/quick_sort.pyExample 2: Importing and using an algorithm
from sorts.quick_sort import quick_sort
# Sort a list
numbers = [64, 34, 25, 12, 22, 11, 90]
sorted_numbers = quick_sort(numbers)
print(sorted_numbers) # Output: [11, 12, 22, 25, 34, 64, 90]Example 3: Running doctests
python -m doctest -v sorts/bubble_sort.pyFor a complete list of all implemented algorithms organized by category, see our DIRECTORY.md file.
We are on Discord and Gitter! Community channels are a great way for you to ask questions and get help. Please join us!
We welcome contributions from the community! Before contributing:
- π Read through our Contribution Guidelines
- π Check existing implementations to avoid duplicates
- β Ensure your code follows our coding standards
- π§ͺ Include doctests and proper documentation
- π― Make sure all tests pass before submitting
Quick Start for Contributors:
# Install pre-commit hooks
pip install pre-commit
pre-commit install
# Run tests
python -m pytest
# Format code
pip install ruff
ruff checkContributions that are most welcome:
- New algorithm implementations
- Improvements to existing algorithms
- Better documentation and explanations
- Bug fixes
- Test coverage improvements
This project is licensed under the MIT License - see the LICENSE.md file for details.
This means you are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software.
See our directory for easier navigation and a better overview of the project.