哪位高手帮忙解决下--折半插入排序(有错误,不会改)
折半插入排序算法性能分析:要求
实现折半插入排序
分别随机生成50、100、500、1000、5000、10000个数据,统计在这些数据上程序的执行时间
以下代码有错误,哪位高手帮忙解决下,谢谢, 急需
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <time.h>
using namespace std;
//const int n = 50;
int n=100;
//scanf("%d",&n);
void swap(int *a,int i,int j)
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void binaryinsertsort(int * a,int n)
{
int i,j,low,high,mid;
for(i=1;i<n;++i)
{
a[0]=a[i];
low = 1;high = i- 1;
while(low <= high)
{
mid = (low+high)/2;
// if(a[mid] == value) return mid; //找到,插在mid前面
if(a[mid]>a[0]) high = mid-1;
else low = mid+1;
}
}
for(j=i-1;j>=high+1;--j) a[j+1] = a[j];
a[high+1]=a[0];
}
void printarray(int * a,int n)
{
cout << "print array "<< endl;
for(int i = 0;i < n;i++)
{
cout << a[i]<< " ";
if((i+1)%10 == 0)
cout << endl;
}
cout << endl;
}
int main(int argc,char *argv[])
{
srand(time(NULL));
double start,end,t;
int a[10000];
start=clock();
for(int i = 0;i < n;i++)
a[i] = rand()%1000;
printarray(a,n);
binaryinsertsort(a,n);
printarray(a,n);
end=clock();
t=end-start;//t=(double)(end-start);
cout<<"t="<<t<<"秒"<<endl;
//cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"S"<<endl;
return 0;
}