#include <stdio.h>
#include <stdlib.h>
int binsearch(int x, int arr[], int left, int right)
{
while (left <= right)
{
int mid = (left+right)/2;
if (arr[mid] == x)
{
return mid;
}
else if (arr[mid] < x)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
int main()
{
int arr[] = { 2, 6, 20, 66, 177, 199, 600 };
int n;
scanf("%d",&n);
int ret = binsearch(n, arr, 0, sizeof(arr) / sizeof(arr[0] - 1));
if (ret != -1)
{
printf("%d\n", arr[ret]);
}
else
{
printf("not exist\n");
}
system("pause");
return 0;
}用c语言实现折半查找函数
原创
©著作权归作者所有:来自51CTO博客作者fun8888888的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
c语言:编写折半查找函数
c语言:编写折半查找函数
c语言:编写折半查找函数 -
折半查找(c语言)
折半查找描述格式样例题解及注释描述给定一个长度为n(n≤10000)的单调递增整数数列,和要
数据结构 c语言 数据 折半查找 数组 -
【C语言】编写一个折半查找函数
编写一个折半函数,查找数列中的数字
C语言 编写一个折半查找函数 -
C语言 二分查找/折半查找
二分查找C语言代码实现
中位数 数组 折半查找
















