数据结构-插入排序-折半插入排序
来源:互联网 发布:酷方 知乎 编辑:程序博客网 时间:2024/05/06 13:59
插入排序:
①直接插入排序。
②折半插入排序。←this
③2-路插入排序。←this
④表插入排序。
⑤希尔排序。
折半插入排序
描述过程:查找的操作可以利用“折半查找”(二分)来实现,在进行插入排序。
伪代码:
输入:n个元素的数组A[1~n] 。
输出:按非降序排列的数组A[1~n]
for i ←2 to n
x ←A[i]
low ←0,high ←i-1
while(low<=high)
m ←(low+high)/2
if(x<A[m]) high=m-1;
else low=m+1;
end while
for j ←i-1 to high+1
A[j+1] ←A[j]
end for
A[j+1] ←x
end for
最好:已按非降序排序,比较次数为:n-1
最坏:元素已按降序排序,比较次数为:n*logn
时间复杂度:O(n2)时间复杂度:O(n2)折半查找只是减少了比较次数,但是元素的移动次数不变
#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int N=10;void BInsertSort(int *a,int st,int ed){for(int i=st+1;i<ed;i++){int x=a[i];int low=st,high=i-1;while(low<=high){int m=(low+high)/2;if(x<a[m]) high=m-1;else low=m+1;}for(int j=i-1;j>=high+1;--j)a[j+1]=a[j];a[high+1]=x;}}int main(){int a[N]={5,6,3,2,1,8,9,7,4,10}; BInsertSort(a,0,N);for(int i=0;i<N;i++){printf("%d ",a[i]);}return 0;}
-------------------------------------------------分隔线-------------------------------------------------
2-路插入排序是在折半插入排序的基础上改进,其目的是减少排序过程中移动记录的次数,但为此需要n个记录的辅助空间。
描述过程:另外再开一个数组,
最坏:关键字最小或者最大记录时,失去优越性
时间复杂度:O(n2)移动记录次数约为n平方/2
具体就不写了。
0 0
- 数据结构-折半插入排序
- [数据结构]折半插入排序
- 数据结构 折半插入排序
- 数据结构-排序: 折半插入排序
- 数据结构-插入排序-折半插入排序
- 数据结构之 折半插入排序
- 数据结构实验 折半插入排序
- 数据结构-插入排序(直接插入排序+折半插入排序)
- 【数据结构】算法10.2 插入排序-折半插入
- 插入排序--折半插入
- 插入排序--折半插入
- C++代码,数据结构-内部排序-插入排序-折半插入排序
- 排序-插入排序-折半插入排序-数据结构(27)
- 排序:折半插入排序
- 排序--折半插入排序
- 排序-折半插入排序
- 【插入排序】折半插入排序
- 插入排序--折半插入排序
- 1018. Public Bike Management (30)
- linux fdisk U盘分区失败
- Exoplayer Developer guide官方文档译文
- PP系数
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
- 数据结构-插入排序-折半插入排序
- Xcode导出ipa包四个选项的意思
- bzoj2001 [Hnoi2010]City 城市建设 动态最小生成树
- oraclesql分页最高效率写法
- dp46上 HDU2084
- Oracle数据库使用PDE文件导出数据和传统EXP/IMP方式的区别
- java中匿名对象的使用
- 使用MediaPlayer获取Http网络流时MediaPlayer的状态调用
- [并查集 杂题] BZOJ 4452 [Cerc2015]Export Estimate