python快速查找算法应用实例
作者:shichen2014 时间:2021-04-17 10:28:13
本文实例讲述了Python快速查找算法的应用,分享给大家供大家参考。
具体实现方法如下:
import random
def partition(list_object,start,end):
random_choice = start
#random.choice(range(start,end+1))
#把这里的start改成random()效率会更高些
x = list_object[random_choice]
i = start
j = end
while True:
while list_object[i] < x and i < end:
i += 1
while list_object[j] > x:
j -= 1
if i >= j:
break
list_object[i],list_object[j] = list_object[j],list_object[i]
print list_object
#list_object[random_choice] = list_object[j]
#list_object[j] = random_choice
return j
def quick_sort(list_object,start,end):
if start < end:
temp = partition(list_object,start,end)
quick_sort(list_object,start,temp-1)
quick_sort(list_object,temp + 1 ,end)
a_list = [69,65,90,37,92,6,28,54]
quick_sort(a_list,0,7)
print a_list
程序测试环境为Python2.7.6
输出结果如下:
[54, 65, 28, 37, 6, 69, 92, 90]
[6, 37, 28, 54, 65, 69, 92, 90]
[6, 37, 28, 54, 65, 69, 92, 90]
[6, 28, 37, 54, 65, 69, 92, 90]
[6, 28, 37, 54, 65, 69, 90, 92]
[6, 28, 37, 54, 65, 69, 90, 92]
希望本文所述对大家的Python程序设计有所帮助。
标签:python,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
整理Python最基本的操作字典的方法
2022-03-01 07:04:38
详解element-ui 表单校验 Rules 配置 常用黑科技
2023-08-17 17:42:47
![](https://img.aspxhome.com/file/2023/6/55916_0s.jpg)
asp如何读取Windows的信息文件(.ini)?
2009-11-20 18:27:00
SQL子查询全接触
2007-08-20 10:51:00
使用Python制作表情包实现换脸功能
2022-01-14 09:10:18
![](https://img.aspxhome.com/file/2023/4/81794_0s.webp)
Pytorch模型定义与深度学习自查手册
2023-02-11 18:30:27
![](https://img.aspxhome.com/file/2023/7/90417_0s.png)
asp如何创建目录?
2009-11-14 20:51:00
使用python制作游戏下载进度条的代码(程序说明见注释)
2023-06-15 00:39:52
![](https://img.aspxhome.com/file/2023/2/59302_0s.png)
CSS和HTML与前端技术层图示
2010-04-05 21:54:00
![](https://img.aspxhome.com/file/UploadPic/20104/5/200851918234142177801-87s.gif)
Python 自动化表单提交实例代码
2022-12-20 06:16:14
![](https://img.aspxhome.com/file/2023/5/77825_0s.png)
《用户体验的要素》摘记
2008-08-04 17:59:00
如何基于Python和Flask编写Prometheus监控
2021-03-21 13:37:13
Python处理字符串的常用函数实例总结
2022-10-19 21:09:39
![](https://img.aspxhome.com/file/2023/5/75175_0s.png)
对pandas中iloc,loc取数据差别及按条件取值的方法详解
2021-06-15 01:58:05
php引用计数器进行垃圾收集机制介绍
2023-10-07 22:41:55
20行Python代码实现一款永久免费PDF编辑工具
2023-11-17 23:51:45
Python基础之常用库常用方法整理
2022-10-30 10:43:26
![](https://img.aspxhome.com/file/2023/7/63897_0s.png)
深入分析PHP引用(&)
2023-11-23 00:42:35
![](https://img.aspxhome.com/file/2023/1/55771_0s.jpg)
Python中struct模块对字节流/二进制流的操作教程
2021-05-10 19:26:56
![](https://img.aspxhome.com/file/2023/2/90342_0s.png)
removeChild的障眼法
2009-12-04 12:49:00