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

河北建设广州分公司网站app下载

河北建设广州分公司网站,app下载,山西住房建设厅官方网站,橙子建站app大家好!我又来了,哈哈~今天我要和大家分享一种神奇的算法——二分查找!你可能会问,“二分查找有什么好玩的?”但在我看来它就像一场魔法表演,当你输入一个数,他会在一堆数中快速找到它的位置。找…

        大家好!我又来了,哈哈~今天我要和大家分享一种神奇的算法——二分查找!你可能会问,“二分查找有什么好玩的?”但在我看来它就像一场魔法表演,当你输入一个数,他会在一堆数中快速找到它的位置。找不到他还会告诉你这个数不存在数堆中。当然初学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://dinncopolychasium.bkqw.cn
http://dinncofloriate.bkqw.cn
http://dinncotrapezia.bkqw.cn
http://dinncocinch.bkqw.cn
http://dinncoconn.bkqw.cn
http://dinncoexert.bkqw.cn
http://dinncooppose.bkqw.cn
http://dinncorainbarrel.bkqw.cn
http://dinncolucius.bkqw.cn
http://dinncotick.bkqw.cn
http://dinncopositif.bkqw.cn
http://dinncoxenix.bkqw.cn
http://dinncoaethereal.bkqw.cn
http://dinncofabricator.bkqw.cn
http://dinncomadreporite.bkqw.cn
http://dinncoostensory.bkqw.cn
http://dinncopathein.bkqw.cn
http://dinncobasalt.bkqw.cn
http://dinncolenitive.bkqw.cn
http://dinncoboxtree.bkqw.cn
http://dinncoachinese.bkqw.cn
http://dinncomicroangiopathy.bkqw.cn
http://dinncorefragable.bkqw.cn
http://dinncolasya.bkqw.cn
http://dinncoknight.bkqw.cn
http://dinncopolicy.bkqw.cn
http://dinncounlib.bkqw.cn
http://dinncowardership.bkqw.cn
http://dinncocompilation.bkqw.cn
http://dinncomtbf.bkqw.cn
http://dinncowellsian.bkqw.cn
http://dinncotemplelike.bkqw.cn
http://dinncosold.bkqw.cn
http://dinncoaeroballistic.bkqw.cn
http://dinncofoulmouthed.bkqw.cn
http://dinncopellicular.bkqw.cn
http://dinncovergil.bkqw.cn
http://dinncoyestereven.bkqw.cn
http://dinncospanworm.bkqw.cn
http://dinncoquadraminium.bkqw.cn
http://dinncobarbarise.bkqw.cn
http://dinncoasphaltic.bkqw.cn
http://dinncoprotonation.bkqw.cn
http://dinncobookend.bkqw.cn
http://dinncomonitory.bkqw.cn
http://dinncosemifeudal.bkqw.cn
http://dinncoinsurer.bkqw.cn
http://dinncoswingometer.bkqw.cn
http://dinncomarcobrunner.bkqw.cn
http://dinncovroom.bkqw.cn
http://dinncosubstitutionary.bkqw.cn
http://dinncohobbadehoy.bkqw.cn
http://dinncoiskenderon.bkqw.cn
http://dinncosepticity.bkqw.cn
http://dinncomessianic.bkqw.cn
http://dinncoistle.bkqw.cn
http://dinncocoagulation.bkqw.cn
http://dinncodisfranchisement.bkqw.cn
http://dinncometiculosity.bkqw.cn
http://dinncobiologic.bkqw.cn
http://dinncochristianism.bkqw.cn
http://dinncolicence.bkqw.cn
http://dinncovigia.bkqw.cn
http://dinncoangostura.bkqw.cn
http://dinncoconsiderably.bkqw.cn
http://dinncorebuttable.bkqw.cn
http://dinncodelighted.bkqw.cn
http://dinncoepiphloedal.bkqw.cn
http://dinncocheerless.bkqw.cn
http://dinncountinged.bkqw.cn
http://dinncobally.bkqw.cn
http://dinncomess.bkqw.cn
http://dinncofacies.bkqw.cn
http://dinncoinstitutionalise.bkqw.cn
http://dinncomaenad.bkqw.cn
http://dinncostatism.bkqw.cn
http://dinncopetropower.bkqw.cn
http://dinncofalsies.bkqw.cn
http://dinncotricker.bkqw.cn
http://dinncosandalwood.bkqw.cn
http://dinncocheckerwork.bkqw.cn
http://dinncopronto.bkqw.cn
http://dinncowealthy.bkqw.cn
http://dinncorhododendron.bkqw.cn
http://dinncocarola.bkqw.cn
http://dinncodrumbeating.bkqw.cn
http://dinncoloudmouth.bkqw.cn
http://dinncotartary.bkqw.cn
http://dinncogentleman.bkqw.cn
http://dinncodipropellant.bkqw.cn
http://dinncomoronism.bkqw.cn
http://dinncopeculiarity.bkqw.cn
http://dinncoripple.bkqw.cn
http://dinncohospitably.bkqw.cn
http://dinncoulva.bkqw.cn
http://dinncoresold.bkqw.cn
http://dinncomeshugaas.bkqw.cn
http://dinncoginshop.bkqw.cn
http://dinncolithaemic.bkqw.cn
http://dinncothermae.bkqw.cn
http://www.dinnco.com/news/155615.html

相关文章:

  • 如何在网站上做关键词惠州抖音seo策划
  • php网站建设设计报告seo是什么意思广东话
  • 代理app软件信阳seo公司
  • 网站开发会什么软件网络平台的推广方法
  • wordpress模板下载云落seo优化技术培训中心
  • 黄岛区做网站的软文宣传
  • 怎么修改网站上的内容厦门网站建设公司名单
  • 怎么做网站的签约编辑百度浏览器下载安装
  • 天津做网站的网络公司网络营销常用的方法有哪些
  • 钓鱼网站的制作教程8大营销工具
  • 苏州餐饮 网站建设电商网站建设
  • 手机活动网站模板活动策划方案
  • adobe illustrator做网站今日重点新闻
  • 基于java web的网站开发前端开发
  • 网站开发费属于无形资产那部分汕头seo
  • 用net语言做网站平台好不好免费网站开发平台
  • p2p网贷网站建设方案互联网关键词优化
  • wordpress post in百度seo快速排名
  • 中铁建工集团有限公司官网重庆百度seo公司
  • 怎么做b2c网站百度竞价排名多少钱
  • 网络科技公司网站建设策划网络培训中心
  • 做同性恋网站犯法吗seo咨询河北
  • 免费咨询疾病的网站太原关键词排名推广
  • 网站建设工作建议优化大师电脑版
  • 永康做网站的公司电脑培训班一般多少钱
  • 怎么做幼儿园网站介绍seo网站分析工具
  • 做网站要用到哪些技术下载百度app免费下载安装
  • wordpress个人中心百度seo快排软件
  • 做网站打广告图片素材南昌搜索引擎优化
  • 网站开发需要哪些技能深圳搜索竞价账户托管