Insertion sort and Merge sort
来源:互联网 发布:教育培训加盟 知乎 编辑:程序博客网 时间:2024/05/21 09:39
Insertion sort(直接插入排序)
pseudocode:
insertion sort (a,n)
for j = 2 to n key = a[j] i = j -1 while i>0 && a[i] >key a[i+i] = a[i] i = i-1 a[i+1] = key
关于o 标记法
数学上: o(g(n)) = {f(n): 存在正常数c1,c2,n0使得对于所有的n>=n0 都有 0<=c1*g(n)<=f(n)<=c2*g(n) }
工程学上: 削减低阶元素;忽略系数
例如: 3N^3 + 90n^2 - 5n +100 = o(n^3)
最坏时间复杂度:最坏情况下,对于每个j都需要排j次: t(n)=∑o(j) =o(n^2)
平均时间复杂度: t(n)=∑o(j/2) =o(n^2)
Merge srot (归并排序)
pseudocode
merge sort a(1,....,n)
if n = 1 ,donerecursively sort a(1,n/2)and a(n/2+1,n)merge the two sort list
归并排序的时间复杂度分析首先,先针对每一步进行单独分析:
if n = 1 ,done o(1)
recursively sort a(1,n/2)and a(n/2+1,n) 2T(n/2)
merge the two sort list o(n)
于是归并排序的时间复杂度为:T(n) = o(1) + 2T(n/2) + o(n)
忽略o(1), 因此归并排序的时间复杂度现在转化为 求解: T(n)= 2T(n/2) + c*n 要求c>0
转化为地归树为
cn
/ \
T(n/2) T(n/2)
进一步
cn
/ \
cn/2 cn/2
/ \ / \
T(n/4) T(n/4) T(n/4) T(n/4)
最终
cn
/ \
cn/2 cn/2 树的深度为lgn
/ \ / \
cn/4 cn/4 cn/4 cn/4
.... .... ... ....
o(1) 共有n个叶子节点,因此 对于叶子节点来说时间复杂度为o(n)
在求最坏时间复杂度时,将树的每一层近似的看成与叶子节点的时间复杂度相同,因此求解归并排序的时间复杂度为 o(nlgn)
- Insertion Sort and Merge Sort
- Insertion sort and Merge sort
- Insertion Sort and Merge Sort
- Insertion sort, select sort, Quick sort and Merge sort
- Insertion Sort, Merge Sort
- Sort List (merge & Insertion)
- Bubble sort, Insertion sort, merge sort, quick sort, heap sort
- Merge Sort Array --> Merge Sort Linked List --> Insertion Sort Array --> Insertion sort Linked List
- Merge Sort and Inversion
- Introduction: Analysis of Algorithms, Insertion Sort, Merge Sort
- 排序算法:Insertion Sort和Merge Sort in GoLang
- Insertion Sort
- Insertion Sort
- insertion sort
- insertion sort
- Insertion Sort
- insertion sort
- insertion sort
- 关于内核空间与用户空间
- C#:PDU格式短信编解码(七)参考资料
- zoj 3396
- 金士顿内存条验证真伪
- URL的分类
- Insertion sort and Merge sort
- 分页代码
- LED点阵
- Oracle中查询前10条记录
- 购物车实现的原理
- break,continue,goto,return语句区别详解
- 配置实现-列表控件
- UVa Problem 10047 The Monocycle(独轮车)
- 开通博客三天内不允许发表文章