顺序查找算法
int lin_Search(const int array[],int size,int key)
{
int index;
for (index = 0;index <= size-1;++index)
if (key==array[index]) return index;
return -1;
}
二分查找算法
int bin_Search(const int array[],int low,int high,int key)
{
int index;
while ( high>= low)
{
index = (low+high)/2;
if (key==array[index])
{
return index;
}
else if (key > array[index])
low = index+1;
else high = index-1;
}
return -1;
}
或者
int binarySearch(int[] E,int first,int las,int K)
{
if (last<first)
index = -1;
else
{
int mid=(first+last)/2;
if(K== E[mid])
index = mid;
else if(K<E[mid])
index = binarySearch(E,first,mid-1,K);
else
index = binarySearch(E,first,mid-1,K);
return index;
}
}