首页 > 编程语言 >推荐:常见算法的python实现(github上25000多star)

推荐:常见算法的python实现(github上25000多star)

时间:2022-12-12 13:34:36浏览次数:79  
标签:sort __ github 25000 python collection input bubble


近日在github上发现一个25000多star的仓库,把各种常见算法用python实现了,而且还有动图演示,非常值得推荐。

仓库说明

这个仓库用python语言实现了绝大部分算法,主要是用于教学目的,因此效率稍微低于工业界。

仓库地址:

​https://github.com/TheAlgorithms/Python​

内容说明

包含了常见的算法的python实现,如二叉树、排序、查找等等。这些是算法工程师必须掌握的技能。

文件目录

推荐:常见算法的python实现(github上25000多star)_机器学习

动画演示

推荐:常见算法的python实现(github上25000多star)_机器学习_02

冒泡排序

推荐:常见算法的python实现(github上25000多star)_github_03

桶排序

推荐:常见算法的python实现(github上25000多star)_github_04

快速排序

典型代码

(这个是冒泡排序的代码):

from __future__ import print_function

def bubble_sort(collection):
"""Pure implementation of bubble sort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
Examples:
>>> bubble_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> bubble_sort([])
[]
>>> bubble_sort([-2, -5, -45])
[-45, -5, -2]

>>> bubble_sort([-23,0,6,-4,34])
[-23,-4,0,6,34]
"""
length = len(collection)
for i in range(length-1):
swapped = False
for j in range(length-1-i):
if collection[j] > collection[j+1]:
swapped = True
collection[j], collection[j+1] = collection[j+1], collection[j]
if not swapped: break # Stop iteration if the collection is sorted.
return collection

if __name__ == '__main__':
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
user_input = raw_input('Enter numbers separated by a comma:').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(*bubble_sort(unsorted), sep=',')

总结

本文推荐了github上一个25000多star的仓库,把各种常见算法用python实现了,而且还有动图演示,非常值得推荐。

仓库地址:

​https://github.com/TheAlgorithms/Python​

请关注和分享↓↓↓ 

标签:sort,__,github,25000,python,collection,input,bubble
From: https://blog.51cto.com/u_15671528/5929485

相关文章

  • 机器学习笔记的github镜像下载(github个人star数量排名175)
    黄海广博士在github开源了机器学习及深度学习个人笔记,成为热门项目,详情报道见​​文章​​。其中机器学习笔记10000+star,深度学习笔记7200+star。个人star数量23310,排名gith......
  • Python的入门学习Day 10~13——form”夜曲编程“
    Day10time:2021.8.7.​今天本来打算学习时发现手机应该拿去充电了,再上完J课程之后发现时间确实只留到了晚上呢。但幸好,以我多天的敲代码的牢固根基(哈哈哈),我最终......
  • Python安装TensorFlow-GPU
    选择TensorFlow版本(重要)验证TensorFlow-gpu安装成功安装遇到的问题参考TOC本文主要介绍windows下基于Miniconda下的GPU版本的TensorFlow安装过程以及安装过程中遇到的问......
  • Python安装Pytorch-GPU
    选择Pytorch版本(重要)验证pytorch安装是否成功安装遇到的问题参考TOC本文主要介绍windows下基于Miniconda下的GPU版本的Pytorch安装过程以及安装过程中遇到的问题,本文假......
  • Python虚拟环境(二):Linux基于Anaconda创建虚拟环境并打包
    1、创建并查看虚拟环境1、创建虚拟环境condacreate-npy37python3.7#创建一个名称为py37的Python版本为3.7的Python虚拟环境#或condacreate-npy37--copyy......
  • Python虚拟环境(一):基于virtualenv+virtualenvwrapper创建python虚拟环境
    1.概述2.virtualenv+virtualenvwrapper1.virtualenv安装virtualenv基本使用2.virtualenvwrapper安装virtualenvwrapper设置环境变量WORKON_HOMEvirtualenvwrapper的......
  • Go/Python gRPC实践
    gRPC框架&ProtoBuf安装相关工具:pip3installgrpciopip3installgrpcio-toolsprotobuf3有自己专门的定义的格式,基于此可以生成不同的脚本编写示例的protobuf3:......
  • GitHub 轻松提速教程 [持续更新]
    GitHub是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub,在天朝Github默认被限速了,我们可以通过修改hosts文......
  • Python 接收解析用户输入参数
    方式一:importsys,getopt;if(__name__=='__main__')or(__name__=='main'):##参数0是文件名+后缀##参数一 Name=sys.argv[1];......
  • 推荐《用Python进行自然语言处理》中文翻译-NLTK配套书
    NLTK配套书《用Python进行自然语言处理》(NaturalLanguageProcessingwithPython)已经出版好几年了,但是国内一直没有翻译的中文版,虽然读英文原版是最好的选择,但是对......