当前位置:网站首页>csdn每日一练——有序表的折半查找
csdn每日一练——有序表的折半查找
2022-06-10 22:35:00 【蓝色满天星星星】
问题描述:
用有序表表示静态查找表时,通常检索函数可以用折半查找来实现。
折半查找的查找过程是:首先确定待查记录所在的范围,然后逐步缩小范围直到找到或者确定找不到相应的记录为止。而每次需要缩小的范围均为上一次的一半,这样的查找过程可以被称为折半查找。
第二行包含n个用空格隔开的正整数,表示n个有序的整数。输入保证这n个整数是从小到大递增的。
第三行包含k个用空格隔开的正整数,表示k次查询的目标。
输出:
只有1行,包含k个整数,分别表示每一次的查询结果。如果在查询中找到了对应的整数,则输出其相应的位置,否则输出-1。
请在每个整数后输出一个空格,并请注意行尾输出换行。
以下程序实现了这一功能,请你填补空白处的内容:
#include <stdio.h>
int binary(int* a, int key, int n)
{
int left = 0, right = n - 1, mid = 0;//第一次查询的left=0,n=19
mid = (left + right) / 2;//第一次二分
while (left < right && a[mid] != key)
{
if (a[mid] > key)//当目标值在中值左边,让此时的中值前一位的序号作为右值序号
{
right = mid - 1;
}
else if (a[mid] < key)//当目标值在中值右边,让此时的中值后一位的序号作为左值序号
{
left= mid +1;
}
mid = (right + left) / 2;//第二次算出中值,可一直循环
}
if (a[mid] == key)
return mid;
return -1;
}
int main(void)
{
int Base_a[20] = { 1, 3, 5, 8, 9, 40, 120, 123, 125, 150, 199, 200, 1250, 1255, 1900, 2000, 2001, 3000, 3950, 5000 };
int Search_a[5] = { 12, 199, 9, 2001, 3500 };
int result = 0x00;
for (int i = 0; i < sizeof(Search_a) / sizeof(Search_a[0]); i++)//sizeof(Search_a) / sizeof(Search_a[0])可获取要查询数组的长度
{
result = binary(Base_a, Search_a[i], sizeof(Base_a) / sizeof(Base_a[0]));//获取整个待索引数组的长度,为20,n=19
printf("[%d %d] ", Search_a[i], result);//输出[目标元素,元素下标],result有-1和mid两种取值方法
}
printf("\n");
return 0;
}
说明:感觉这个跟二分法原理一致,我写的注释可能有错误,如有发现,请指出,如果对你有帮助的话,点个赞鼓励一下吧!
边栏推荐
- C语言创建动态二维数组
- How to generate automatic references (simple drawings)
- The shell script of pagoda plan task regularly deletes all files under a directory [record]
- Fiddler creates an autoresponder
- Perfect decoding purecodec 20220601
- BGP - route map extension (explanation + configuration)
- Create millisecond time id in LabVIEW
- Two debugging techniques in embedded software development
- 已知某种木材的横纹抗压力服从N(x,d2),现对十个试件作横纹抗压力试验,得数据如下:(单位:kg/cm2)
- iframe框架自适应大小/全屏显示网页框架的方法
猜你喜欢

LabVIEW change the shape or color of point ROI overlay

vtk.js中vtp下载

Analysis of Genesis public chain

LeetCode+ 16 - 20

PHP实现iframe跨站替换文字/替换iframe网站文字的方法

Fiddler filtering sessions

Data and information resource sharing platform (VII)

Developers changing the world - Yao Guang teenagers playing Tetris

可扩展到Max–MCU和MPU开发,使用相同的许可证

Vs tomato assistant add header comments and usage
随机推荐
Error 1046 when LabVIEW uses MathScript node or matlab script
R language to draw two-dimensional normal distribution density surface;
LabVIEW programming specification
Is qiniu's securities account true? Is it safe?
【二叉树】二叉树剪枝
LabVIEW和VDM提取色彩和生成灰度图像
Introduction to Wireshark capturing RTP load TS stream (UDP multicast)
Solve access denied for user 'root' @ 'localhost' (using password: yes)
LabVIEW phase locked loop (PLL)
Unity script cannot display Chinese comments of C # source code or the script created by vs does not have comments of C # source code
Interview questions - written examination
Yuntu says that every successful business system cannot be separated from apig
PHP implementation of iframe cross site text replacement / iframe website text replacement
Dell R730 raid5 安装Server 2016(解决磁盘大于2T)
Why is the kotlin language not popular now?
LabVIEW打开其他EXE程序
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
LabVIEW get IMAQ get last event coordinates
Leetcode 501: mode dans l'arbre de recherche binaire
数据文件nc6oa.txt由33个癌细胞系得6830个基因表达数据构成,每一个细胞系都是某种类型的癌细胞的类型。请按照基因表达数据对33个细胞系进行聚类(聚类类别数划是癌细胞的类型个数,比如乳腺癌、肺