当前位置: 编程技术>c/c++/嵌入式
纯C语言:折半查找源码分享
来源: 互联网 发布时间:2014-10-23
本文导语: 代码如下:#include int bin_search(int key[],int low, int high,int k) { int mid; if(low>high) { return -1; } else { mid = (low+high) / 2; if(key[mid]==k)...
代码如下:
#include
int bin_search(int key[],int low, int high,int k)
{
int mid;
if(low>high)
{
return -1;
}
else
{
mid = (low+high) / 2;
if(key[mid]==k)
return mid;
if(k>key[mid])
return bin_search(key,mid+1,high,k); /*在序列的后半部分查找*/
else
return bin_search(key,low,mid-1,k); /*在序列的前半部分查找*/
}
}
int main()
{
int n , i , addr;
int A[10] = {2,3,5,7,8,10,12,15,19,21};
printf("The contents of the Array A[10] aren");
for(i=0;i