insertion mergesort bubble sort 复习 python
来源:互联网 发布:阿列克谢耶维奇 知乎 编辑:程序博客网 时间:2024/05/16 11:27
最近重新开始复习算法那本书,整理了一下关于insertion mergesort以及 divide and conquer的思路
insertion sort is O(n^2) best case when it is already sorted it will be O(n) worse case is the array is reversed sorted.
the main idea is we put an iterator in the array and make sure everything infront of the iterator is sorted.
for index in range(1,len(array)):
key=arrray[index]
j=index
while j>0 and array[j-1]>key://如果比当前的大就往后移动,得到的是递增的序列
array[j]=array[j-1]
j-=1
array[index]=key
def insertion(num): for index in range(1,len(num)): key=num[index] j=index while j>0 and num[j-1]>key: num[j]=num[j-1] j-=1 num[j]=key
关于mergesort的思路是将整个array每次除以2往下分,直到长度为1,然后依次排列
所以整个array可以分成
n
n/2 n/2
n/4 n/4 n/4 n/4
.....一共log(n)层
每层排列需要n次运算因此为nlogn
def mergesort(num): if len(num)==1: return num def merge(left,right): merged=[] while left and right: merged.append(left.pop(0) if left[0]<=right[0] else right.pop(0)) while left: merged.append(left.pop(0)) while right: merged.append(right.pop(0)) return merged mid=len(num)/2 left=mergesort(num[:mid]) right=mergesort(num[mid:]) return merge(left,right)
除此以外bubble sort是最最基本的算法。
就不具体阐述了。
def bubble(num): for i in range(len(num)-1): for j in range(len(num)-i-1): if num[j+1]>num[j]: num[j+1],num[j]=num[j],num[j+1] return num
radix sort.
def radixsort(num): radix=10 tmp,placement=-1,1 maxlen=False while not maxlen: maxlen=True buckets=[list() for _ in range(radix)] for i in num: tmp=i/placement buckets[tmp%radix].append(i) if maxlen and tmp>0: maxlen=False a=0 for b in range(radix): buck=buckets[b] for i in buck: num[a]=i a+=1 placement*=radix print numdef main(): num=[18,5,100,3,1,19,6,0,7,4,2] radixsort(num) print numif __name__=="__main__": main()
heap sort
def siftdown(a,start,end): root=start while 2*root+1<end: child = 2*root+1 if child+1<=end and a[child]<a[child+1]: child+=1 if a[root]<a[child]: a[root],a[child]=a[child],a[root] root=child else: returndef heapify(a): start=(len(a)-2)/2 while start>=0: siftdown(a,start,len(a)-1) start-=1 end=len(a)-1 while end>0: a[end],a[0]=a[0],a[end] end-=1 siftdown(a,0,end) return adef main(): num=[8,1,2,9,11,10,4,5,100] print heapify(num)if __name__=="__main__": main()
0 0
- insertion mergesort bubble sort 复习 python
- Bubble sort, Insertion sort, merge sort, quick sort, heap sort
- Bubble, Insertion, Selection, Quick and Heap Sort
- Python-Bubble Sort
- python insertion sort
- [Python]Bubble Sort冒泡排序
- LeetCode-Insertion Sort List (Python)
- [leetcode] Insertion Sort List(python)
- Insertion Sort List Leetcode Python
- [Leetcode][python]Insertion Sort List
- 【Data Structures】 7. Simple Sorting—Bubble Sort, Selection Sort, and Insertion Sort
- Bubble sort
- Bubble Sort
- bubble sort
- Bubble Sort
- Bubble Sort
- bubble sort
- Bubble Sort
- 2014上海全国邀请赛Maze
- protobuf 使用相关
- 路径和文件处理
- JQuery小练习
- spring mvc和jquery uploadify 404
- insertion mergesort bubble sort 复习 python
- 偶尔文艺-关于想要转录MIDI从YAMAHA到MAC
- ubuntu启动时加载的服务
- BZOJ1009 [HNOI2008]GT考试(KMP算法+矩阵加速dp)
- javascript--&& II 问题总结
- 【设计模式】-单例模式(Singleton)
- 终端密码输入问题
- Iphone 6&6p,IOS8适配工作总结(2015-2-7更新)
- POJ 3785 The Next Permutation 全排列字典序法