已知:無序數組,折半查找,各元素值唯一。
函數原型是:Binary_Seach(int array[], int iValue, int iCount)
array是數組,在里面用折半查找的方法找等于iValue的值,找到返回1否則0,iCount是元素個數,如何做呢?
把插入排序和折半查找一起做:
int Binary_Seach(int array[], int iValue, int iCount)
{
int i,low,high,tmp,m,j;
for(i=2;i<=iCount;++i)
{
tmp=array[i];
low=1; high=i-1;
while(low<=high){
m=(low+high)/2;
if(array[m]==iValue) return 1;
if(tmp else low=m+1;
}
for(j=i-1;j>=high+1;--j) array[j+1]=array[j];
array[high+1]=tmp;
}
return 0;
}