Python实现常见算法[2]——快速排序

#!/usr/bin/python

# module: quik_sort.py

def PARTION(L,m,n):
base = L[n]
i = m-1
j = m
while j<n:
if L[j] < base:
i = i+1
temp = L[i]
L[i] = L[j]
L[j] = temp
j = j+1
i = i+1
temp = L[i]
L[i] = L[n]
L[n] = temp
return i def QUIK_SORT(L,x,y):
if (x>=y):
return
p = PARTION(L,x,y)
QUIK_SORT(L,x,p-1)
QUIK_SORT(L,p+1,y)

算法验证代码

#!/usr/bin/python

import random
import quik_sort L = range(100)
random.shuffle(L) quik_sort.QUIK_SORT(L, 0, len(L)-1)
print L
上一篇:try-catch


下一篇:【0003】SD卡烧写镜像