当前位置:网站首页>对半查找(折半查找)
对半查找(折半查找)
2022-06-27 13:09:00 【fitpolo】
//非递归方式解决
#include <stdio.h>
//返回下标
int binary_search(int *pdata,int size,int x)
{
int low,hight,mid;
low = 0;
hight = size-1;
while(low <= hight)
{
mid = (hight + low)/2;
if (pdata[mid] < x)
{
low = mid + 1;
}else if (pdata[mid] > x)
{
hight = mid - 1;
}else
{
return mid;
}
}
return -1;
}
//递归方法解决
//递归方法
int binary_search_recursion(int *pdata,int hight,int low,int x)
{
int mid=0;
if (low > hight)
{
printf("-1hight;%d-low:%d\r\n",hight,low);
return -1;
}
//printf("hight;%d-low:%d\r\n",hight,low);
mid = (hight + low)/2;
if (pdata[mid]<x)
{
binary_search_recursion(pdata,hight,mid + 1,x);
} else if (pdata[mid]>x)
{
binary_search_recursion(pdata,mid - 1,low,x);
}else
{
return mid;
}
}
//测试代码
int main(int argc, char *argv[])
{
int result;
#define BUF_SIZE 10
int buf[BUF_SIZE] = {
1,3,5,7,9,11,13,15,17,20};
//result = binary_search(buf,BUF_SIZE,20);
result = binary_search_recursion(buf,9,0,20);
printf("result:%d\r\n",result);
//result = binary_search(buf,BUF_SIZE,19);
result = binary_search_recursion(buf,BUF_SIZE-1,0,19);
printf("result:%d\r\n",result);
result = binary_search_recursion(buf,BUF_SIZE-1,0,1);
printf("result:%d\r\n",result);
return 0;
}
运行结果
边栏推荐
- ThreadLocal 源码全详解(ThreadLocalMap)
- MySQL 索引及其分类
- 每日刷題記錄 (六)
- AI for Science: scientific research paradigm, open source platform and industrial form
- Shake hands with life and make peace
- Local visualization tool connects to redis of Alibaba cloud CentOS server
- Pycharm in Chinese
- 内网学习笔记(8)
- 阿里一个面试题:使用两个线程,交替输出字母和数字
- 【周赛复盘】LeetCode第81场双周赛
猜你喜欢

今日睡眠质量记录78分

抖音实战~公开/私密短视频互转

Does Xinhua San still have to rely on ICT to realize its 100 billion enterprise dream?

An interesting experiment of netmask

How to download pictures with hyperlinks

IJCAI 2022 | greatly improve the effect of zero sample learning method with one line of code. Nanjing Institute of Technology & Oxford proposed the plug and play classifier module

阿里一个面试题:使用两个线程,交替输出字母和数字

Using FRP tool to realize intranet penetration

Vs debugging skills

Yuweng information, a well-known information security manufacturer, joined the dragon lizard community to build an open source ecosystem
随机推荐
Cloud native (30) | kubernetes' app store Helm
ViewPager2使用记录
Details of istio micro service governance grid traffic management core resource controller
Openfeign service interface call
Convn-n dimensional convolution
Threejs' ambient light + point light + parallel light + spherical light and Hepler understanding + shadow ()
内网学习笔记(8)
Istio微服务治理网格流量管理核心资源控制器详解
一道shell脚本的统计题
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
Openhgnn releases version 0.3
数字化新星何为低代码?何为无代码
微服务如何拆分
Full explanation of ThreadLocal source code (threadlocalmap)
Database Series: MySQL index optimization and performance improvement summary (comprehensive version)
Airbnb复盘微服务
Local visualization tool connects to redis of Alibaba cloud CentOS server
【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(一)
gcc编译动态库和静态库
Prometheus 2.26.0 新特性