纯C语言:折半查找源码分享
时间:2021-05-01 09:33:55|栏目:C代码|点击: 次
复制代码 代码如下:
#include <stdio.h>
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] are\n");
for(i=0;i<10;i++)
printf("%d ",A[i]); /*显示数组A中的内容*/
printf("\nPlease input a interger for search\n");
scanf("%d",&n); /*输入待查找的元素*/
addr = bin_search(A,0,9,n);
if(-1 != addr) /*查找成功*/
{
printf("%d is at the %dth unit is array A\n ",n,addr);
}
else
{
printf("There is no %d in array A\n",n); /*查找失败*/
}
getchar();
return 0;
}
上一篇:C++动态内存分配(new/new[]和delete/delete[])详解
栏 目:C代码
下一篇:C语言实现猜数字游戏
本文标题:纯C语言:折半查找源码分享
本文地址:http://www.codeinn.net/misctech/112537.html