项目介绍

TheAlgorithms/Python 是 GitHub 上拥有超过 218,000 颗星标的顶级开源项目,由 The Algorithms 组织维护。该项目旨在用 Python 语言实现各种经典算法和数据结构,为全球开发者和学生提供一个高质量的算法学习资源。

所有实现均以教育目的为主,代码清晰易懂,并附有详细注释。虽然在性能上可能不如 Python 标准库中的实现,但它们是理解算法原理的绝佳参考。

项目信息 详情
GitHub 星标218,570+
编程语言Python
算法类别40+ 个大类
开源许可MIT License
维护组织The Algorithms
代码风格Ruff 格式化

算法分类

该项目涵盖了计算机科学中几乎所有主要的算法类别,共计 40 余个分类:

数学与分析

算术分析、线性代数、矩阵运算、数学计算、布尔代数

搜索与排序

各类搜索算法、排序算法(冒泡、快排、归并等)、字符串匹配(KMP)

数据结构

链表、树、图、栈、队列、堆、哈希表等经典数据结构

算法范式

回溯法、分治法、动态规划、贪心算法、位运算

人工智能

机器学习、神经网络、遗传算法、模糊逻辑、计算机视觉

密码与安全

各类密码学算法、哈希算法、区块链基础实现

图论

图遍历、最短路径、最小生成树、网络流等图算法

其他领域

数字图像处理、压缩算法、分形、量子计算、调度算法、物理模拟、金融计算

主要特点

  • 覆盖全面 - 涵盖 40+ 个算法分类,从基础排序到高级机器学习应有尽有
  • 教育导向 - 代码注释详尽,命名清晰,专为学习理解而设计
  • 纯 Python 实现 - 无需额外依赖,使用标准 Python 即可运行绝大部分代码
  • 持续更新 - 活跃的社区持续贡献新的算法实现和优化
  • 严格质量控制 - 使用 pre-commit、Ruff 格式化和 CI 自动化测试保证代码质量
  • 社区支持 - 通过 Discord 和 Gitter 获取帮助,与全球开发者交流
  • 开源免费 - MIT 许可证,可自由使用、学习和修改
  • Gitpod 集成 - 支持一键在线编辑和运行代码,无需本地环境配置

快速开始

要使用该项目中的算法,你可以按照以下步骤操作:

1. 克隆仓库

git clone https://github.com/TheAlgorithms/Python.git cd Python

2. 浏览算法目录

项目提供了详细的 算法目录(DIRECTORY.md),方便你快速查找所需算法。

3. 运行示例

# 运行某个算法示例 python sorts/bubble_sort.py # 或者在 Python 交互环境中导入 python >>> from sorts.bubble_sort import bubble_sort >>> bubble_sort([5, 3, 8, 1, 2]) [1, 2, 3, 5, 8]

4. 在线编辑

你也可以通过 Gitpod 一键在线打开项目,无需本地安装任何环境。

5. 参与贡献

阅读 贡献指南,了解如何为项目添加新的算法实现。

社区议题

以下是该项目当前开放的 Issue(按评论数排序),共 39 条,标题已翻译为中文供参考。

编号 议题标题(中文翻译 / 英文原文) 创建日期 评论
#8818 修复 failing: `pytest quantum/q_fourier_transform.py`
Fix failing: `pytest quantum/q_fourier_transform.py`
缺陷
2023-06-13 20
#12809 需要 help for my age controller
Need help for my age controller
增强
2025-06-26 16
#12192 topological sort returns reversed 列表
topological sort returns reversed list
缺陷
2024-10-19 15
#13008 Suggestion: 添加 basic crypto price converter
Suggestion: add basic crypto price converter
awaiting triage
2025-09-29 14
#6216 Some algorithms are "how-to" 示例
Some algorithms are "how-to" examples
需要帮助
2022-06-23 13
#13226 缺陷: Inconsistent behavior for 缺失 values in coordinate_compression.py
Bug: Inconsistent behavior for missing values in coordinate_compression.py
缺陷
2025-10-05 12
#8847 `machine_learning/linear_regression.py` doesn't give optimal coefficients
`machine_learning/linear_regression.py` doesn't give optimal coefficients
增强
2023-06-30 12
#13904 Favour Chidinma - Python project
Favour Chidinma - Python project
awaiting triage
2025-11-13 10
#6252 Suggestion: adopt a 文档 format for docstring
Suggestion: adopt a documentation format for docstring
2022-07-14 10
#12714 Calc.py
Calc.py
2025-05-08 9
#13845 添加 input validation for division by zero in divide_numbers()
Add input validation for division by zero in divide_numbers()
增强
2025-11-03 8
#12233 Avoid log(0) in KL divergence
Avoid log(0) in KL divergence
缺陷
2024-10-21 8
#12127 添加 binary 搜索
Add binary search
awaiting triage
2024-10-17 8
#10713 More detailed information on 测试 failures in Actions 页面
More detailed information on test failures in Actions page
增强
2023-10-19 8
#8594 Optimize Project Euler solutions
Optimize Project Euler solutions
增强需要帮助optimization
2023-04-01 8
#13509 缺陷
Bugs
缺陷
2025-10-15 7
#13304 Hangman game program in python
Hangman game program in python
awaiting triage
2025-10-07 7
#12987 Snake water gun game program in python
Snake water gun game program in python
2025-09-22 7
#12968 SYNTAX 错误
SYNTAX ERROR
缺陷
2025-09-15 7
#10837 Nested functions can't be tested by doctest
Nested functions can't be tested by doctest
增强
2023-10-23 7
#10563 `pytest` 警告 in builds
`pytest` warnings in builds
缺陷
2023-10-15 7
#13417 添加 some more programs
Adding some more programs
增强
2025-10-10 6
#12622 添加 PID and ADRC algorithm
Add PID and ADRC algorithm
增强
2025-03-17 6
#11517 想要 to 添加 sliding window algorithm
Want to add sliding window algorithm
增强
2024-08-14 6
#13445 Issue Title: 添加 示例 Code Using Popular Python Libraries
Issue Title: Add Example Code Using Popular Python Libraries
增强
2025-10-12 5
#13320 想要 to 添加 [ML] 实现 PCA, Logistic Regression (Vectorized), Naive Bayes with Laplace Smoothing, and Decision Tree Pruning from Scratch
Want to add [ML] Implement PCA, Logistic Regression (Vectorized), Naive Bayes with Laplace Smoothing, and Decision Tree Pruning from Scratch
增强
2025-10-07 5
#12569 添加 a collision detection algorithm
Add a collision detection algorithm
增强
2025-02-09 5
#12379 Find:audio_filters/butterworth_filter.py issure
Find:audio_filters/butterworth_filter.py issure
缺陷
2024-11-17 5
#10703 Duplicate algorithms in `maths/primelib.py`
Duplicate algorithms in `maths/primelib.py`
增强
2023-10-19 5
#8070 `mypy` produces 错误 when run on its own
`mypy` produces errors when run on its own
增强
2023-01-03 5
#14050 Python Mini Project: Grocery Store Cart System I built a simple console-based Grocery Store application using Python 🐍 to strengthen my understanding of core programming concepts. 🔧 Features: 添加 items to cart 移除 items from cart Calculate total price dynamically Menu-driven program using loops 📚 Concepts Used: Dictionaries Loops (while) Conditional statements User input handling This project helped me practice logic building and real-world problem solving using Python. 💡 More improvements coming soon! #Python #Programming #MiniProject #LearningByDoing #AIML #EngineeringStudent
Python Mini Project: Grocery Store Cart System I built a simple console-based Grocery Store application using Python 🐍 to strengthen my understanding of core programming concepts. 🔧 Features: Add items to cart Remove items from cart Calculate total price dynamically Menu-driven program using loops 📚 Concepts Used: Dictionaries Loops (while) Conditional statements User input handling This project helped me practice logic building and real-world problem solving using Python. 💡 More improvements coming soon! #Python #Programming #MiniProject #LearningByDoing #AIML #EngineeringStudent
2025-12-28 4
#13840 不正确 output for Binary 搜索 when 列表 contains duplicates
Incorrect output for Binary Search when list contains duplicates
缺陷
2025-11-02 4
#13714 添加 “Usage 示例” section to README
Add “Usage Example” section to README
增强
2025-10-23 4
#13372 添加 Vision Transformer (ViT) Demo to computer_vision Module
Add Vision Transformer (ViT) Demo to computer_vision Module
增强
2025-10-09 4
#13315 添加 Pattern printing programmes
Add Pattern printing programmes
awaiting triage
2025-10-07 4
#13205 Upgrade Linear Regression with Fully Vectorized 实现 and 添加 a naive 实现 for beginners
Upgrade Linear Regression with Fully Vectorized Implementation and add a naive implementation for beginners
增强
2025-10-05 4
#12949 data type syntax 错误
data type syntax error
缺陷
2025-09-03 4
#12513 错误 regarding imports in replit
Error regarding imports in replit
awaiting triage
2025-01-08 4
#12321 添加 ART1 algorithm in neural network
Add ART1 algorithm in neural network
增强
2024-10-29 4

常见问题 (FAQ)

这个项目适合什么水平的学习者?
适合所有水平的学习者。初学者可以从基础排序和搜索算法开始,中级开发者可以学习动态规划和图算法,高级用户可以研究机器学习和量子计算相关的实现。
这些算法实现可以直接用于生产环境吗?
这些实现主要用于教育目的,可能不如 Python 标准库或专业库(如 NumPy、SciPy)中的实现高效。建议在生产环境中使用经过充分测试和优化的库,而将本项目作为学习和理解算法的参考。
如何为项目做贡献?
首先阅读项目的贡献指南(CONTRIBUTING.md),然后 fork 仓库,在本地实现新的算法或改进现有代码,确保通过所有测试后提交 Pull Request。项目使用 Ruff 进行代码格式化,并要求通过 CI 测试。
项目需要哪些前置条件?
你只需要安装 Python(建议 3.8 及以上版本)。大部分算法使用纯 Python 实现,不需要额外依赖。部分高级算法(如机器学习)可能需要安装 NumPy 等库。
如何在项目中找到我需要的算法?
项目提供了详细的 DIRECTORY.md 文件,按类别组织了所有算法。你也可以使用 GitHub 的搜索功能在仓库中直接搜索算法名称。此外,The Algorithms 官网也提供了便捷的浏览方式。
有社区支持吗?
有的!你可以加入 The Algorithms 的 Discord 服务器或 Gitter 聊天室,与全球的算法爱好者交流,提问和获取帮助。