insert插入排序 最坏O(n2)平均O(n2)
shell希尔排序(不稳定) 最坏O(n2)
bubble冒泡排序 最坏O(n2)平均O(n2)
merge归并排序 最坏O(nlg(n)) 平均O(nlg(n))
quick快速排序 最坏 O(nlg(n))平均O(nlg(n))
heap堆排序 最坏O(nlg(n))平均O(nlg(n))
count计数排序 最坏O(n+k)平均O(n+k)
radix基数排序 最坏O(n+k)平均O(n+k)
bucket桶排序 最坏O(n+k) 平均O(n+k)
插入、希尔、冒泡、归并、堆以及快速排序都是基于值比较的排序, 最坏情况的下线是nLg(n),堆排序和归并排序是渐进最优的比较排序算法.shell排序的时间复杂度是依赖于argument sequence 的,所以你用不同的序列,时间复杂度不同n^(1.3)是一个比较快的实现
计数排序通过使用数组索引作为相对次序的工具,在O(k+n)的时间内完成n个数的排序。 当k=O(n)时,计数排序的运行时间与输入数组的规模呈线性关系。
基数排序,可用来扩展计数排序的使用范围。如果有n个整数要进行排序,每个整数有d位数字,并且每个数字可能有k个值,那么基数排序就可以在O(d*(n+k))时间内完成排序。 当k=O(n),基数排序的运行时间也是线性的
桶排序,需要了解输入数组中数据的概率分布,对于半开区间【0,1)内服从均匀分布的n个实数,桶排序的平均运行时间为O(n)
- insert插入排序
def insert_sort(L):
for i in xrange(1, len(L)):
j = i - 1
value = L[i]
while j >= 0 and L[j] > value:
L[j+1] = L[j]
j -= 1
L[j+1] = value
- shell希尔排序
def shell_sort(L):
step = len(L) / 2
while step >= 1:
for i in xrange(step, len(L)):
j = i - step
value = L[i]
while j >=0 and L[j] > value:
L[j+step] = L[j]
j -= step
L[j+step] = value
step /= 2
- bubble冒泡排序
def bubble_sort(L):
n = len(L)
while n > 1:
for i in xrange(n-1):
if L[i] > L[i+1]:
L[i], L[i+1] = L[i+1], L[i]
n -= 1
- merge归并排序
def merge_sort(L, begin, end):
if begin < end:
mid = (begin+end)/2
merge_sort(L, begin, mid)
merge_sort(L, mid+1, end)
merge(L, begin, mid, end)
def merge(L, begin, mid, end):
i, j = begin, mid+1
temp = []
while i<=mid and j<=end:
if L[i] <= L[j]:
temp.append(L[i])
i += 1
else:
temp.append(L[j])
j += 1
while i<=mid:
temp.append(L[i])
i += 1
while j <= end:
temp.append(L[j])
j += 1
i = begin
for v in temp:
L[i] = v
i += 1
- quick快速排序
def quick_sort(L, begin, end):
if begin < end:
pos = partition(L, begin, end)
quick_sort(L, begin, pos-1)
quick_sort(L, pos+1, end)
def partition(L, begin, end):
i, j = begin, end
while i < j:
while i < j and L[i] < L[j]:
j -= 1
if i < j:
L[i], L[j] = L[j], L[i]
i += 1
while i < j and L[i] < L[j]:
i += 1
if i < j:
L[i], L[j] = L[j], L[i]
j -= 1
return i
- heap堆排序
def heap_sort(L):
n = len(L)
L.insert(0, None)
build_heap(L, n)
while n > 1:
L[1], L[n] = L[n], L[1]
n -= 1
heapify(L, 1, n)
del L[0]
def build_heap(L, n):
for i in xrange(n/2, 0, -1):
heapify(L, i, n)
def heapify(L, i, n):
lchild = i*2
rchild = i*2 + 1
max_index = i
if lchild <= n and L[lchild] > L[max_index]:
max_index = lchild
if rchild <= n and L[rchild] > L[max_index]:
max_index = rchild
if max_index != i:
L[max_index], L[i] = L[i], L[max_index]
heapify(L, max_index, n)
- count计数排序
k = 100
def count_sort(L):
'''集合[0, 99]内的数字排序'''
B, C = [], [0] * k
B[:] = L
for v in B:
C[v] += 1
for i in xrange(1, k):
C[i] += C[i-1]
for v in B[::-1]:
L[C[v]-1] = v
C[v] -= 1
- radix基数排序
def radix_sort(L, d):
'''暂时要求L由整数构成'''
k = 10
def count_sort(L, pos):
tempL, B, C = [], [], [0]*k
tempL[:]= L
B[:] = [v%10**pos/10**(pos-1) for v in L]
for v in B:
C[v] += 1
for i in xrange(1, k):
C[i] += C[i-1]
# B 与 tempL 是基于位置上一一对应的关系
for v, value in zip(B[::-1], tempL[::-1]):
L[C[v]-1] = value
C[v] -= 1
for i in xrange(1, d+1):
count_sort(L, i)
- bucket桶排序
def bucket_sort(L):
def insert_sorted(L, value):
for i, v in enumerate(L):
if value < v:
L.insert(i, value)
return
else:
L.append(value)
length = 10
tempL = []
# tempL中的元素用于定位在B中的位置
tempL[:] = [v/length for v in L]
# 初始化B数组, 数组长度为length [[],[],[],...[]]
B = []
for i in xrange(max(tempL)+1):
B.append([])
for v, value in zip(tempL, L):
insert_sorted(B[v], value)
i = 0
for lis in B:
for v in lis:
L[i] = v
i += 1