折半插入排序

来源:互联网 发布:常熟淘宝培训 编辑:程序博客网 时间:2024/06/18 09:27
#include<iostream>using namespace std;const int MAX=300;void BInsertSort(int * arry,int n);bool LG(int a,int b);void show(int *arry,int n){for(int i=1;i<=n;i++){if(i!=1)cout<<" ";cout<<arry[i];}}int main(){int n,temp,i=0;int arry[MAX];  cin>>n;temp=n;while(temp--){cin>>arry[++i];}BInsertSort(arry,n);show(arry,n);return 0;}void BInsertSort(int * arry,int n){int low,high,m,j;for(int i=2;i<=n;i++){low=1;high=i-1;arry[0]=arry[i];//减少比较次数 while(low<=high){m=(low+high)/2;if(LG(arry[i],arry[m]))high=m-1;elselow=m+1;}//减少移动次数 for(j=i-1;j>=high+1;j--)arry[j+1]=arry[j];arry[high+1]=arry[0];}}bool LG(int a,int b){return a<b;}

0 0
原创粉丝点击