python 排序
来源:互联网 发布:qq for mac截图快捷键 编辑:程序博客网 时间:2024/06/03 20:04
insert插入排序 最坏
shell希尔排序(不稳定) 最坏
bubble冒泡排序 最坏
merge归并排序 最坏
quick快速排序 最坏
heap堆排序 最坏
count计数排序 最坏
radix基数排序 最坏
bucket桶排序 最坏
插入、希尔、冒泡、归并、堆以及快速排序都是基于值比较的排序, 最坏情况的下线是
计数排序通过使用数组索引作为相对次序的工具,在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 = 100def 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
- python 排序
- python 排序
- Python排序
- python 排序
- Python 排序
- 排序Python
- Python 排序
- python 排序
- python 排序
- python 排序
- python 排序
- Python 排序
- Python-排序
- python 排序
- Python 排序
- python排序
- python 排序
- Python排序
- AVR中断引起重启
- NS2常用命令总结
- 数据结构总结(5)
- JAVA中this与super两个关键字的用法
- 最详细的Log4j使用教程
- python 排序
- 扩展activiti使用自己的用户表和群组表
- getRequestDispatcher()与sendRedirect()的区别
- 数据结构习题--线性表
- java并发内存模型
- 4.0 第十一章 Linq
- 实现计时器 的简单demo
- [accept 4 ] what the difference between accept4 and accept
- 自定义轮播图控件