Python实现的几种排序(来自wiki百科)
2014-04-14 13:53
148 查看
1.归并排序:
2.堆排序
def merge(l1,l2): final=[] #对l1,l2进行排序 l1 = merge(l1) l2 = merge(l2) while l1 and l2: if l1[0]<=l2[0]: final.append(l1.pop(0)) else: final.append(l2.pop(0)) return final+l1+l2
2.堆排序
#!/usr/bin/env python #-*-coding:utf-8-*- def heap_sort(lst): for start in range((len(lst)-2)/2,-1,-1): sift_down(lst,start,len(lst)-1) for end in range(len(lst)-1,0,-1): lst[0],lst[end] = lst[end],lst[0] sift_down(lst,0,end-1) return lst def sift_down(lst,start,end): root = start while True: child = 2*root + 1 if child > end:break if child+1 <= end and lst[child] < lst[child+1]: child += 1 if lst[root] < lst[child]: lst[root],lst[child] = lst[child],lst[root] root = child else: break def main(): l = [9,2,1,7,6,8,5,3,4] heap_sort(l) if __name__ == "__main__": main()
相关文章推荐
- Python实现几种常见排序代码
- 《面试》 几种经典的简单的排序方法(冒泡,选择,插入)python实现
- js几种常见排序的实现
- 快速排序(伪代码 c/c++ python 实现)
- Python中的单例模式的几种实现方式的及优化
- Python递归实现快速排序
- Python3中实现数值取整的几种方法
- python实现2种简单的排序算法--冒泡排序和选择排序
- 基数排序(Python实现出现的奇怪问题???)
- Python 快速排序 堆排序——Python实现一些算法持续更新
- Python几种并发实现方案的性能比较
- 快速排序,归并排序,堆排序python实现
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- 选择排序Python3实现
- 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)
- Python实现字典的遍历与排序功能示例
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- python 实现插入排序,冒泡排序,快速排序,选择排序
- Python实现快速排序和插入排序算法及自定义排序的示例
- 用C语言实现常用的几种排序