python 冒泡和快排,不多说
2024-09-01 06:00:32
#-*-coding:utf8-*-
import random
a=[]
b=[]
def init_array():
for i in range(10000):
v = random.randint(1,10000)
a.append(v)
b.append(v) #冒泡
def sort_array(a):
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i] > a[j]:
i_v = a[i]
a[i]=a[j]
a[j]=i_v #快排
def quick_sort(a,left,right):
if left < right:
key = a[left]
low = left
high = right
while low<high:
while low<high and a[high]>=key:
high-=1
a[low] = a[high]
while low<high and a[low]<=key:
low+=1
a[high]=a[low]
a[low]=key
quick_sort(a,left,low-1)
quick_sort(a,low+1,right) init_array() print "a",len(a)
print "b",len(b) sort_array(a)
quick_sort(b,0,len(b)-1) print a,len(a)
print b,len(b) 以上保存为 sort.py,然后:
python -m cProfile sort.py 冒泡时间复杂度O(n^2)
快排最差O(n^2),平均O(n*lgn)
最新文章
- 李洪强iOS经典面试题156 - Runtime详解(面试必备)
- windows2003安装证书服务:csp配置不正确、您没有此密钥容器的写访问权限
- CSS3-样式继承,层叠管理,文本格式化
- linux 中的斜杠 ‘/’ ‘\’ 的区别 - 服务器是LINUX的注意了, 和windows 的不一样哦!
- 笔记:ASP.NET MVC安全
- mysql 远程访问授权
- 编写一函数用来实现左右循环移位。函数原型为move(value,n);n>;0时右移n位,n<;0时左移|n|位。
- 浅析Android中的消息机制
- pyqt 简单判断指定的内容强度(比如帐号)
- 配置Raspbian 启用SPI I2C
- C++模板:字典树
- Android(Lollipop/5.0) Material Design(六) 使用图像
- 2. SQL Server数据库状态监控 - 错误日志
- ontimer 与多线程
- 2014NOIP普及组 子矩阵
- cmd代码:查端口占用,查进程号,杀进程
- 有了这8款Mac安全杀毒和流氓防护软件,让你的mac清理优化,更加安全
- win10家庭版多用户
- 最新版Kali Linux虚拟机安装Open-vm-tools替代VMware tools
- C# 读取sqlite文件