折半插入排序

来源:百度知道 编辑:UC知道 时间:2024/05/30 06:02:38
要求用C语言实现
一个数组double a[10]={1,2,3,4,5,6,7,8,9,10}
插入数x
写一个程序

#include <iostream>
using namespace std;
void BiInsert_sort(double p[],double x);
void main()
{
double arr[12]={0,1,2,3,4,5,6,7,8,9,10,0};
cout<<"输入要插入的数: ";
double x;
cin>>x;
BiInsert_sort(arr,x);
for (int i=0;i<=11;i++)
{
cout<<arr[i]<<endl;
}
}
void BiInsert_sort(double p[],double x)
{
p[0] = x;
int low = 1,high = 10;
while(low<=high)
{
int mid = (low+high)/2;
if (p[0]<p[mid])
high = mid-1;
else
low=mid+1;
}
for (int j=10;j>high;j--)
p[j+1] = p[j];
p[j+1] = p[0];

}

试一下这段程序满足你要求吗,数组arr[0]不用,从arr[1]开始排的。

已知数组已经排好序了?

再插就不要用折半查找了吧!