print("冒泡排序")

def bubbleSort(input_list):‘‘‘函数说明:冒泡排序(升序)

:param input_lis: 待排序列表

:return:sorted_list :升序排好的列表‘‘‘ if len(input_list) == 0:return[]

sorted_list=input_listfor i in range(len(sorted_list)-1):

bchanged=False

# print("第%d趟排序:"%(i-1))for j in range(len(sorted_list)-1):if sorted_list[j + 1]

sorted_list[j],sorted_list[j+1] = sorted_list[j+1],sorted_list[j]

bchanged=True

# print(sorted_list)ifnot bchanged :break

returnsorted_listif __name__ == ‘__main__‘:

input_list=[50,2,4,5,99,44,33,66,77,88]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)

print(‘直接插入排序‘)

def inserSort(input_list):‘‘‘函数说明:直接插入排序

:param input_list: 待排序的列表

:return: sorted_list 排序号的列表‘‘‘ if len(input_list) == 0:return[]

sorted_list=input_listfor i in range(1,len(sorted_list)):

temp=sorted_list[i]

j= i -1

while j >=0 and temp

sorted_list[j+1] =sorted_list[j]

j-=1sorted_list[j+1] =tempreturnsorted_listif __name__ == ‘__main__‘:

input_list= [6,4,8,9,2,3,1]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)

print("希尔排序")

def shellSort(input_list):

length=len(input_list)if length <=1:returninput_list

sorted_list=input_list

gap= length //2

while gap > 0:for i inrange(gap,length):

j= i -gap

temp=sorted_list[i]while j >= 0 and temp

sorted_list[j+gap] =sorted_list[j]

j-=gap

sorted_list[j+ gap] =temp

gap//= 2

returnsorted_listif __name__ == ‘__main__‘:

input_list= [50,123,53,154,34,6,7,46,78,67]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)‘‘‘快速排序‘‘‘def QuickSort(input_list , left,right):‘‘‘函数说明:快速排序(升序)

:param input_list:

:param left:

:param right:

:return:‘‘‘def division(input_list,left,right):‘‘‘函数说明:根据left和right进行一次扫描,重新找到基准数

:param input_list: 待排序列

:param left: 左指针位置

:param right: 右指针位置

:return: left 新的基准位置‘‘‘ base =input_list[left]while left =base:

right-=1input_list[left]=input_list[right]while left < right and input_list[left] <=base:

left+=1input_list[right]=input_list[left]

input_list[left]= base

returnleftif left

base_index=division(input_list,left,right)

QuickSort(input_list,left,base_index- 1)

QuickSort(input_list,base_index+ 1,right )if __name__ == ‘__main__‘:

input_list= [6,3,4,6,7,8,2,1]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)

print("简单选择排序")

def Selectsort(input_list):‘‘‘函数说明:简单选择排序(升序)

:param input_list: 待排序列表

:return: sortde_list : 升序排好序的列表‘‘‘ if len(input_list) == 0:return[]

sorted_list=input_list

length=len(sorted_list)for i inrange(length):

min_index= 1

for j in range(i + 1,length):if sorted_list[min_index] >sorted_list[j]:

min_index=j

temp=sorted_list[i]

sorted_list[i]=sorted_list[min_index]

sorted_list[min_index]=tempreturnsorted_listif __name__ =="__main__":

input_list=[4,3,1,6,7,8,9,5,2]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)

print("堆序列")

def HeadSort(input_list):‘‘‘函数说明:堆排序(升序)

:param input_list: 待排序列表

:return: sorted_list:升序排序好的列表‘‘‘def HeadAdjust(input_list,parent,length):‘‘‘函数说明:堆调整,调整为最大堆

:param input_list: 待排序列表

:param parent: 堆的父节点

:param length: 数组长度

:return: 无‘‘‘ temp =input_list[parent]

child= 2*parent +1

while child

child+=1

if temp >=input_list[child]:breakinput_list[parent]=input_list[child]

parent=child

child= 2 *parent +1input_list[parent]=tempif len(input_list) == 0:return[]

sorted_list=input_list

length=len(sorted_list)for i in range(0,length //2 +1 )[::-1]:

HeadAdjust(sorted_list,i ,length)for j in range(1,length)[::-1]:

temp=sorted_list[j]

sorted_list[j]= sorted_list[0]

sorted_list[0] =temp

HeadAdjust(sorted_list,0,j )

print("第%d趟排序:"%(length-j ),end="")

print(sorted_list)returnsorted_listif __name__ == ‘__main__‘:

input_list= [3,5,6,7,8,21,2,1]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)

print("归并排序")

def MergeSort(input_list):‘‘‘函数说明:归并排序(升序)

:param input_list: 待排序列表

:return: sorted_list:升序排序好的列表‘‘‘def merge(input_list,left,mid,right,temp):‘‘‘函数说明:合并函数

:param input_list:待合并列表

:param left: 左指针

:param mid:

:param right:右指针

:param temp: 临时列表

:return:‘‘‘ i =left

j= mid +1k= 0

while i <= mid and j <=right :if input_list[i] <=input_list[j]:

temp[k]=input_list[i]

i+=1

else:

temp[k]=input_list[j]

j+=1k+=1

while i <=mid:

temp[k]=input_list[i]

i+=1k+=1

while j <=right:

temp[k]=input_list[j]

j+=1k+=1def merge_sort(input_list,left,right,temp):if left >=right:returnmid= (right + left) //2

merge_sort(input_list,left,mid,temp)

merge_sort(input_list,mid+1,right,temp)

merge(input_list,left,mid,right,temp)if len(input_list) == 0:return[]

sorted_list=input_list

temp= [0]*len(sorted_list)

merge_sort(sorted_list,0,len(sorted_list)-1,temp)returnsorted_listif __name__ == ‘__main__‘:

input_list= [3,5,6,2,1,0,4,8,9]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

print("-"*50)

print("基数排序")

def RadixSort(input_list):‘‘‘函数说明:基数排序(升序)

:param input_list: 待排序列表

:return: sorted_list 升序排序好的列表‘‘‘def MaxBit(input_list):‘‘‘函数说明:求出数组中最大数的位数的函数

:param input_list: 待排序列表

:return: bits_num 位数‘‘‘ max_data =max(input_list)

bits_num= 0

whilemax_data :

bits_num+=1max_data//= 10

returnbits_num

def digit(num,d):‘‘‘函数说明:取数xxx数上的第d位数字

:param num: 带操作的数

:param d: 第d位的数

:return: 取数结果‘‘‘ p = 1

while d > 1:

d-=1p*=10

return num //p % 10

if len(input_list) ==0:return[]

sorted_list=input_list

lenght=len(sorted_list)

bucket= [0]*lenghtfor i in range(1,MaxBit(sorted_list)+1):

count=[0]*10

for i in range(0,lenght):

count[i]+= count[i -1]for i in range(0,lenght)[::-1]:

k=digit(sorted_list[i],d)

bucket[count[k]-1] =sorted_list[i]

count[k]-=1

for i in range(0,lenght):

sorted_list[i]=bucket[i]returnsorted_listif __name__ == ‘__main__‘:

input_list=[50,123,543,134,5,76,8,6,9,89,56,45]

print("排序前",input_list)

sorted_list=bubbleSort(input_list)

print("排序后",sorted_list)

python八大排序算法 间书_python八大排序算法相关推荐

  1. python八大排序算法 间书_Python 八大排序算法速度比较

    这篇文章并不是介绍排序算法原理的,纯粹是想比较一下各种排序算法在真实场景下的运行速度. 算法由 Python 实现,用到了一些语法糖,可能会和其他语言有些区别,仅当参考就好. 测试的数据是自动生成的, ...

  2. python的算法是指_python中的算法

    算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制.也就是说,能够对一定规范的输入,在有限时间内获得所要求的输 ...

  3. python常用算法有哪些_python常见排序算法基础教程

    前言:前两天腾讯笔试受到1万点暴击,感觉浪费我两天时间去牛客网做题--这篇博客介绍几种简单/常见的排序算法,算是整理下. 时间复杂度 (1)时间频度一个算法执行所耗费的时间,从理论上是不能算出来的,必 ...

  4. python与c进程间通讯_python 与c通信

    Python多进程并行编程实践-mpi4py的使用 前言 在高性能计算的项目中我们通常都会使用效率更高的编译型的语言例如C.C++.Fortran等,但是由于Python的灵活性和易用性使得它在发展和 ...

  5. python基础知识笔记简书_Python基础学习笔记

    Python貌似有点火热,上手还是比较简单的,自己找了个教程也偷偷的学习一下,扒了一下网上的图片和数据,感觉并不是很难呀(不过之前换电脑,代码丢了,有点可惜,不过网上教程一抓一大把,随便看看也能扒一些 ...

  6. python对excel操作简书_Python实现EXCEL常用操作——pandas简介

    知乎的代码块太丑了,这里的内容就更新到简书了Python实现EXCEL常用操作--pandas简介​www.jianshu.com EXCEL是日常办公最常用的软件,然而遇到数据量特别大(超过10W条 ...

  7. python开发人员看什么书_python初学者看什么书

    虽然我不是Python高手,但我是零基础,之前会的都是软件PS,PPT之类.点击链接加入群[我爱python大神]:https://jq.qq.com/?_wv=1027&k=47zuLPd ...

  8. python牛顿迭代法求根例题_python求根算法

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! scipy官网:https:www.scipy.org这个库是python科学计 ...

  9. python基础知识笔记简书_Python学习笔记01——基础知识

    Python常用数据类型: 整数(int),浮点数(float),字符串(str) 布尔型(True,False),空值(None) 列表(list):一种有序集合,可以随时添加删除其中的元素. cl ...

最新文章

  1. java 循环list 对象_java计算list集合中重复对象的次数及for循环内外创建对象
  2. 解决java.lang.IllegalStateException: getOutputStream() has already been called for this response
  3. 通过ip快速定位问题主机连接的交换机
  4. HDU 2003 求绝对值
  5. handsontable 方法汇总
  6. BugkuCTF-Crypto题一段Base64
  7. FusionChart实现柱状图、饼状图的动态数据显示 附Demo
  8. atitit. it软件项目管理---自己的员工,雇佣军、援军,混合的员工 杂牌 人员管理架构
  9. 【第六届蓝桥杯】奇妙的数字
  10. 暨阳社区创始人游牧:为什么我们要转型?
  11. 织梦制作二级全国分站教程,多城市分站插件代码调用
  12. nbu客户端卸载_在LINUX系统下如何卸载NetBackup
  13. .yml文件的基本用法
  14. 回复整理 080307
  15. 掘地三尺搞定 Redis 与 MySQL 数据一致性问题
  16. 贤胜足球分析系统 v2.4.4 大小球测试版 怎么用
  17. 资料 | 20个必不可少的Python库
  18. 通过图形界面对MySQL数据库进行操作
  19. Not Found - GET https://registry.npmjs.org/@vue%2fvue-loader-v15 - Not found
  20. douphp mysql版本_DouPHP轻量级企业建站系统下载

热门文章

  1. 2022-2028年中国热塑性聚酯PBT工程塑料行业市场全景调查及发展趋势分析报告
  2. python中的新式类与旧式类的一些基于descriptor的概念(下)
  3. Redis 使用技巧
  4. LeetCode简单题之找出数组的最大公约数
  5. LeetCode简单题之赎金信
  6. 最大限度地减少块输出中间结果的计算和存储
  7. 2021年大数据Hadoop(三):Hadoop国内外应用
  8. 安装PHP7.3.2make编译出现报错,内存不足导致,临时解决方法
  9. Make sure no other Soong process is using it
  10. Linux批量查找与替换