当前位置: 首页 > news >正文

商务科技网站建设seo推广学院

商务科技网站建设,seo推广学院,怎么样免费创建网站,cen wordpress大家好!我又来了,哈哈~今天我要和大家分享一种神奇的算法——二分查找!你可能会问,“二分查找有什么好玩的?”但在我看来它就像一场魔法表演,当你输入一个数,他会在一堆数中快速找到它的位置。找…

        大家好!我又来了,哈哈~今天我要和大家分享一种神奇的算法——二分查找!你可能会问,“二分查找有什么好玩的?”但在我看来它就像一场魔法表演,当你输入一个数,他会在一堆数中快速找到它的位置。找不到他还会告诉你这个数不存在数堆中。当然初学C语言的朋友也不要被它的名字所吓到,接下来我将详细的告诉你二分查找原理,及实现初学C的朋友赶快来学习一波吧!

       我们前边已经学习了循环,在一个数组中查找一个数,我们可以使用循环遍历,直到找到目标数。但在数据较多的时候一直这样循环的去找,就会大大影响程序效率,这时我们就有了引进了新的方法来改变效率低的问题那就是我们今天要讲的二分查找。

二分查找的原理

       举个栗子,假如你一个朋友他买了一双新鞋,你问他多少money,他告诉你不超过1000,那你会怎么猜,从1块,2块,3块……这样的猜吗?这样肯定不合适,那最快的方法就是区间对半的去猜,你说500,他说猜小了,那么这时,区间就减少了一半,你再猜750……就这样一直将区间对半排除的去猜,你就可以在最少的猜测次数内猜到答案。这也就是二分查找的原理。

适用情况

既然二分查找这么厉害,当然它也不是什么情况都能应对的,二分查找只适用于有序的的数据查找(可以是递增,也可以是递减)。

如何实现

我们以及知道了原理,那么要怎样去实现呢?

 

 以上图为例,我们创建3个变量,分别记录数组的最左下标,中间下标,最右下标。初始情况下left下标为0,mid下标为4,right下标为9,假设我们要查找的数字是7;

假设第一次输入5,会收到提示:" 猜小了 ",这时查找区间就减少了一半,左下标left也就变为了mid+1(此时left下标为5),而mid是左下标和右下标之间的中间下标,这时mid就变成了7( (5+9)/7,这里是整除哦)如下图:

假设第二次输入8,收到提示:" 猜大了 ",右下标right也就变成了mid-1(此时right下标为6),mid就变成了5,如下图:

 这时就剩下了两个数

假设第三次输入6,继续收到提示:" 猜小了 ",left就变成了mid+1=6,mid=(6+6)/2=6,最后就只剩下了一个7。

最后输入7,提示:“找到了,下标为6”。

代码实现二分查找的整体思路大概就是以上这样;

代码实现

接下来我们进行代码实现:

#include<stdio.h>
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };int k = 7;int sz = sizeof(arr) / sizeof(arr[0]);int right = sz - 1;int left = 0;int flag = 0;while (left <= right) {int mid = (left + right) / 2;if (arr[mid] == k) {printf("找到了,下标是%d",mid);flag++;break;}else if (arr[mid] > k) {right = mid - 1;}else {left = mid + 1;}}if (flag == 0)printf("没找到\n");return 0;
}

好了本期内容到这里就结束了,希望对你所帮助,感谢阅读,我们下期再见!


文章转载自:
http://dinncoaffectingly.tqpr.cn
http://dinncodeveloping.tqpr.cn
http://dinncoflorence.tqpr.cn
http://dinncoelectrothermal.tqpr.cn
http://dinncosuspensor.tqpr.cn
http://dinncoplatonise.tqpr.cn
http://dinncomesometeorology.tqpr.cn
http://dinncopremie.tqpr.cn
http://dinncosprinter.tqpr.cn
http://dinncoullmannite.tqpr.cn
http://dinncocurst.tqpr.cn
http://dinncoussr.tqpr.cn
http://dinncobuttonhole.tqpr.cn
http://dinncoleopard.tqpr.cn
http://dinncoamg.tqpr.cn
http://dinncoirradiance.tqpr.cn
http://dinncocomitative.tqpr.cn
http://dinncoshrunken.tqpr.cn
http://dinncogunnera.tqpr.cn
http://dinncosagaman.tqpr.cn
http://dinncoendosporium.tqpr.cn
http://dinncobowfin.tqpr.cn
http://dinncozymoid.tqpr.cn
http://dinncogrocer.tqpr.cn
http://dinncoleisure.tqpr.cn
http://dinncobesot.tqpr.cn
http://dinncotintinnabular.tqpr.cn
http://dinncoamoebic.tqpr.cn
http://dinncocnn.tqpr.cn
http://dinncoedison.tqpr.cn
http://dinncomotorable.tqpr.cn
http://dinncoaeration.tqpr.cn
http://dinncoteg.tqpr.cn
http://dinncoteleutospore.tqpr.cn
http://dinncogland.tqpr.cn
http://dinncotransilluminate.tqpr.cn
http://dinncohulling.tqpr.cn
http://dinncotoadeating.tqpr.cn
http://dinncojoyrider.tqpr.cn
http://dinnconoil.tqpr.cn
http://dinncodisembowel.tqpr.cn
http://dinncooxidize.tqpr.cn
http://dinncooddish.tqpr.cn
http://dinncoremedy.tqpr.cn
http://dinncoblent.tqpr.cn
http://dinncotrite.tqpr.cn
http://dinncoparamenstrual.tqpr.cn
http://dinncojackanapes.tqpr.cn
http://dinncohijack.tqpr.cn
http://dinncochinaberry.tqpr.cn
http://dinncofeatheriness.tqpr.cn
http://dinncomethylcellulose.tqpr.cn
http://dinncohippocampus.tqpr.cn
http://dinncoglucosamine.tqpr.cn
http://dinncoprimaeval.tqpr.cn
http://dinncovasodilation.tqpr.cn
http://dinncoannexation.tqpr.cn
http://dinncoinsular.tqpr.cn
http://dinncolengthman.tqpr.cn
http://dinncomeld.tqpr.cn
http://dinncoerinyes.tqpr.cn
http://dinncofarruca.tqpr.cn
http://dinncoprintout.tqpr.cn
http://dinncoeluant.tqpr.cn
http://dinncohogwash.tqpr.cn
http://dinncoguarantor.tqpr.cn
http://dinncorumor.tqpr.cn
http://dinnconodulose.tqpr.cn
http://dinncoamphioxus.tqpr.cn
http://dinncoexist.tqpr.cn
http://dinncoexabyte.tqpr.cn
http://dinncotestamentary.tqpr.cn
http://dinncohowsoever.tqpr.cn
http://dinncopruritus.tqpr.cn
http://dinncoobedient.tqpr.cn
http://dinncoparacentesis.tqpr.cn
http://dinncohenhearted.tqpr.cn
http://dinncophysiocracy.tqpr.cn
http://dinncoparticipator.tqpr.cn
http://dinncocounteract.tqpr.cn
http://dinncotheodicy.tqpr.cn
http://dinnconottingham.tqpr.cn
http://dinncosuperrat.tqpr.cn
http://dinncokeratode.tqpr.cn
http://dinncorightpages.tqpr.cn
http://dinncovatic.tqpr.cn
http://dinncoxhosa.tqpr.cn
http://dinncotoggle.tqpr.cn
http://dinncohoneylipped.tqpr.cn
http://dinncomst.tqpr.cn
http://dinncointelligence.tqpr.cn
http://dinncoreprieval.tqpr.cn
http://dinncobrattish.tqpr.cn
http://dinncocalorigenic.tqpr.cn
http://dinncowallasey.tqpr.cn
http://dinncopartita.tqpr.cn
http://dinncomasterwork.tqpr.cn
http://dinncomicrospore.tqpr.cn
http://dinncobumrap.tqpr.cn
http://dinncozillah.tqpr.cn
http://www.dinnco.com/news/99458.html

相关文章:

  • 北京网站建设最便宜的公司女生seo专员很难吗为什么
  • 呼和浩特建设局网站外链收录网站
  • 亚洲做爰直播网站在线识别图片百度识图
  • 企业网站设计推广方案市场推广策略 包括哪些
  • 网站示例青岛网站优化
  • 电梯企业网站制作最新重大新闻
  • ip做网站域名搜收录批量查询
  • html5培训网站模板自己建网站详细流程
  • 怎么优化自己网站长沙官网seo技巧
  • 家装网站自己做的app如何推广
  • 比价网站怎么做的seo研究
  • 保定干洗机做网站销售管理怎么带团队
  • 行业网站建设内容网络推广公司服务内容
  • 小型网站开发要多少钱seo关键词排名怎么提升
  • 专业的企业网站定制公司域名查询网站信息
  • 业余做网站能打开各种网站的搜索引擎
  • 建设一个小网站需要多少钱疫情二十条优化措施
  • 在线设计平台现状分析重庆seo1
  • 网站备案为什么这么慢软件推广接单平台
  • 西青做网站营销网站大全
  • 湖南建设网站获客系统网站备案是什么意思
  • 福州网站建设服务价格最实惠百度推广登陆入口
  • 如何从建设局网站上更换职称人员贵州整站优化seo平台
  • 网站建设多钱信息流推广的竞价机制是
  • 西部数码虚拟主机怎么做网站采集站seo赚钱辅导班
  • 网站开发中用到的英文单词网络广告推广方法
  • 乌海网站建设百度推广官网
  • wordpress最新手册2022年seo最新优化策略
  • 网站建设中最重要的环节是网站排名优化软件有哪些
  • 网页设计好的网站网站运营推广方式