#include <stdio.h>
#include <time.h>
#define CLOCKS_PER_SEC ((clock_t)1000)
int binsearch(int, int array[], int n);
int main()
{
int array[] = {2, 5, 6, 8, 9, 11, 12, 13, 14, 15, 16};
int tag = 9;
int res = -1;
clock_t start;
clock_t finish;
start = clock();
res = binsearch(9, array, 11);
finish = clock();
printf("%ld
%ld
", start, finish);
printf("%d
%f
", res, (double)(finish-start)/CLOCKS_PER_SEC);
return 0;
}
int binsearch(int tag, int array[], int n)//对顺序数组,进行折半查找
{
int low = 0;
int high = n-1;
int mid = 0;
long i = 4500000;
while(i--);
while(low <= high)
{
mid = (low+high)/2;
if(tag > array[mid])
{
low = mid + 1;
}
else if(tag < array[mid])
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}