您的位置:首页 > 其它

蓝桥杯基础练习 数列排序

2017-02-12 21:47 274 查看
#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;
else
low=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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: