python 冒泡和快排,不多说【无聊】

 #-*-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)
上一篇:ssh远程连接一段时间会失效的问题


下一篇:安装“Microsoft SQL Server 2014 Management Objects”时报错"Error Writing to file: Microsoft.SqlServer.XEvent.Linq.dll."