首页 > 代码库 > python部分排序算法(网友提供)
python部分排序算法(网友提供)
// 冒泡排序
def bubble(x,n):
‘‘‘This function orders the original items x
x is list,n is the length of x‘‘‘
for i in range(n):
for j in range(n-1):
if x[j] >x[j+1]:
t = x[j]
x[j]= x[j+1]
x[j+1] = t
// 插入排序
def insert(x,n):
i = 1
while i<n-1:
key = x[i]
j = i-1
while j>=0 and key<x[j]:
x[j+1]= x[j]
j -= 1
x[j+1] = key
i += 1
// 选择排序
def select(x,n):
for i in range(n-1):
key = i
for j in range(i+1,n):
if x[j] < x[key]:
key = j
if key!=i:
t = x[i]
x[i] = x[key]
x[key] = t
// 快速排序
def partition(x,low,high):
key = x[low]
while low<high:
while low<high and x[high]>=key:
high -= 1
if low < high:
x[low]= x[high]
low += 1
while low <high and x[low]<=key:
low += 1
if low < high:
x[high] = x[low]
high -= 1
x[low] = key
return low
def quick(x,low,high):
if low < high:
p = partition(x,low,high)
quick(x,low,p-1)
quick(x,p+1,high)
python部分排序算法(网友提供)