python实现常见的五种排序算法
概要
算法理论讲解有专业的书籍和视频资源,本篇文章主要展示算法排序的python语言描述,具体讲解的资源地址参见文末参考引用
冒泡排序(Bubble Sort)
# 冒泡排序
def bubbleSort(seq=None, reversed=False):
lens = len(seq)
for i in range(lens):
for j in range(lens - i - 1):
if (seq[j] < seq[j + 1] if reversed else seq[i] > seq[j]):
seq[j], seq[j + 1] = seq[j + 1], seq[j]
return seq
if __name__=="__main__":
#打印结果为:[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(bubbleSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13],True))
选择排序(Selection Sort)
# 选择排序
def selectionSort(seq=None, reversed=False):
lens = len(seq)
for i in range(lens):
min_index = i
for j in range(i + 1, lens):
if (seq[min_index] < seq[j] if reversed else seq[i] > seq[j]):
min_index = j
seq[i], seq[min_index] = seq[min_index], seq[i]
return seq
if __name__ == "__main__":
# 打印结果为:[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(selectionSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13], True))
插入排序(Insertion Sort)
# 插入排序
def insertionSort(seq=None, reversed=False):
lens = len(seq)
for i in range(1, lens):
key = seq[i]
j = i
while j > 0 and (seq[j - 1] < seq[j] if reversed else seq[j - 1] > seq[j]):
seq[j], seq[j - 1] = seq[j - 1], seq[j]
j -= 1
return seq
if __name__ == "__main__":
# 打印结果为:[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(insertionSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13], True))
归并排序(Selection Sort)
# 归并排序(分)
def mergeSort(seq):
if len(seq) < 2:
return seq
mid = len(seq) // 2
left = mergeSort(seq[:mid])
right = mergeSort(seq[mid:])
return merge(left, right)
# 归并排序(治)
def merge(left, right):
if not len(left) or not len(right):
return left or right
result = []
i, j = 0, 0
while (len(result) < len(left) + len(right)):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
if i == len(left) or j == len(right):
result.extend(left[i:] or right[j:])
break
return result
if __name__ == "__main__":
# 打印结果为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print(mergeSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13]))
快速排序(Selection Sort)
# 快速排序
def quickSort(seq, start, end):
if start < end:
split = partition(seq, start, end)
quickSort(seq, start, split - 1)
quickSort(seq, split + 1, end)
return seq
def partition(seq, start, end):
pivot_index = start - 1
for i in range(start, end):
# 选择最右边的为pivot
if seq[i] < seq[end]:
pivot_index += 1
seq[pivot_index], seq[i] = seq[i], seq[pivot_index]
seq[end], seq[pivot_index + 1] = seq[pivot_index + 1], seq[end]
return pivot_index + 1
if __name__ == "__main__":
# 打印结果为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print(quickSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13], 0, 15))
参考引用
1, sole learn,ios、android均可免费下载
2, github源文件地址
3,北大公开课 算法设计与分析 屈婉玲教授
4,数据结构-浙江大学
5,算法(普林斯顿大学)
geek成长
记录python学习点滴
12 声望
1 粉丝
推荐阅读
[学习笔记] 官方入门cifar-cnn代码改良和踩坑
摘要 整个例子是官方入门例子,也是我使用tensorflow训练的第一个模型 改进 重构代码,使用类和方法调用,jupyter notebook能方便展示,但我个人喜欢通过编译器调试和运行,所以进行封装 重写load_data方法,内置...
Geekrun阅读 1.2k
基于Sanic的微服务基础架构
使用python做web开发面临的一个最大的问题就是性能,在解决C10K问题上显的有点吃力。有些异步框架Tornado、Twisted、Gevent 等就是为了解决性能问题。这些框架在性能上有些提升,但是也出现了各种古怪的问题难以...
jysong赞 6阅读 4k评论 3
滚蛋吧,正则表达式!
你是不是也有这样的操作,比如你需要使用「电子邮箱正则表达式」,首先想到的就是直接百度上搜索一个,然后采用 CV 大法神奇地接入到你的代码中?
良许赞 4阅读 2.3k
又一款眼前一亮的Linux终端工具!
今天给大家介绍一款最近发现的功能十分强大,颜值非常高的一款终端工具。这个神器我是在其他公众号文章上看到的,但他们都没把它的强大之处介绍明白,所以我自己体验一波后,再向大家分享自己的体验。
良许赞 5阅读 1.8k
00 后清华学霸用 AI 打败大气层「魔法攻击」,还原宇宙真面貌
内容一览:从诞生的那一刻起,人类对宇宙的探索就从未停止。如今,这门古老的科学再次借助 AI 获得加速度。本文将展示 AI 与天文学的结合擦出了怎样的火花。关键词:AI 天文图像 弱引力透镜
超神经HyperAI阅读 86.1k
FastAPI性能碾压Flask?
不止一次的听过,FastAPI性能碾压Flask,直追Golang,不过一直没有测试过,今天闲着没事测试一下看看结果。不知道是哪里出了问题,结果大跌眼镜。
二毛erma0赞 2阅读 10.2k评论 3
【TVM 学习资料】快速入门:编译深度学习模型
这个例子展示了如何用 Relay Python 前端构建神经网络,并为装有 TVM 的 NVIDIA GPU 生成 runtime 库。注意,构建 TVM 需要启用 CUDA 和 LLVM。
超神经HyperAI阅读 34.3k
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。