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

离线推广网站规划书免费建站建站abc网站

离线推广网站规划书,免费建站建站abc网站,做棋牌辅助网站,免费素材库网站目录 什么是二分查找 一、左闭右闭写法[left,right] 代码演示: 二、左闭右开写法[left,right] 代码演示: 今天进行了二分查找的学习。 什么是二分查找 二分查找(Binary Search)是一种常用的搜索算法,也被称为折…

目录

什么是二分查找

 一、左闭右闭写法[left,right]

 代码演示:

二、左闭右开写法[left,right] 

代码演示: 


今天进行了二分查找的学习。 

什么是二分查找

二分查找(Binary Search)是一种常用的搜索算法,也被称为折半查找。它用于在已排序的数组中查找特定元素的位置,通过反复将待查找范围缩小为一半来提高效率。

以下是二分查找的一般步骤:

  1. 确定搜索范围:首先,确定要搜索的数组的起始和结束位置。通常,这是整个数组的起始和结束。

  2. 计算中间位置:计算中间位置的索引,即 (start + end) / 2。

  3. 比较中间元素:将要查找的元素与中间位置的元素进行比较。

    • 如果要查找的元素等于中间位置的元素,那么找到了目标,返回中间位置的索引。
    • 如果要查找的元素小于中间位置的元素,那么说明目标在左半部分,将搜索范围缩小为左半部分。
    • 如果要查找的元素大于中间位置的元素,那么说明目标在右半部分,将搜索范围缩小为右半部分。
  4. 重复步骤2和步骤3,直到找到目标元素或搜索范围为空。如果搜索范围为空,说明目标元素不在数组中。

二分查找的时间复杂度为O(log n),其中n是数组的长度。这是因为每次迭代都将搜索范围缩小为一半,所以在最坏情况下,需要进行log n次迭代才能找到目标元素。

二分查找通常用于已排序的数组,例如升序排列的整数数组或字母表中的单词。它是一种高效的查找算法,适用于大型数据集。

 一、左闭右闭写法[left,right]

定义target是在区间[left,right]里面的,所以有如下两点:middle=(left+right)/2;

  • while( left <= right ),应该使用<=,因为是一个左闭右闭的区间。例:[1,1],此时while循环应当用<=.
  • if( nums[middle] > target ),此时right应该赋值为middle-1;因为当前这个nums[middle]⼀定不是target,那么接下来要查找的左区间结束下标位置就是 middle - 1

 代码演示:

class Solution {
public:int search(vector<int>& nums, int target) {int left = 0;                 // 定义左边界int right = nums.size() - 1;  // 定义右边界while (left <= right) {int middle = left + (right - left) / 2;  // 计算中间位置,避免整数溢出if (nums[middle] == target) {return middle;  // 找到目标,返回索引} else if (nums[middle] > target) {right = middle - 1;  // 目标在左半部分,更新右边界} else {left = middle + 1;  // 目标在右半部分,更新左边界}}return -1;  // 如果未找到目标元素}
};

        在计算中间位置时,一种最直观的方法是使用 (left + right) / 2。然而,这种方式在极端情况下,当 leftright 很大时,可能会导致整数溢出问题,这会导致程序错误。

为了避免整数溢出,我们使用了 (right - left) / 2,而不是 (left + right) / 2 来计算中间位置。这样做的原因是,(right - left) 表示了左边界和右边界之间的距离,然后除以2,得到的结果就是中间位置相对于左边界的偏移量。这个偏移量被加到左边界上,从而得到中间位置。

        这种方式确保了中间位置的计算不会导致整数溢出,因为它始终处理整数边界的相对偏移量,而不是绝对值。这在处理大数组时特别重要,以确保算法的正确性。

二、左闭右开写法[left,right] 

定义 target 是在⼀个在左闭右开的区间⾥,也就是[left, right) ,那么二分法的边界处理⽅式则截然不同。
有如下两点:

  • while (left < right),这里使用 < ,因为left == right在区间[left, right)是没有意义的
  • if (nums[middle] > target) right 更新为 middle,因为当前nums[middle]不等于target,去左区间继续寻找,而寻找区间是左闭右开区间,所以right更新为middle,即:下⼀个查询区间不会去比较nums[middle],

代码演示: 

class Solution {
public:int search(vector<int>& nums, int target) {int left = 0;                 // 定义左边界int right = nums.size();      // 定义右边界,注意这里是 nums.size(),不再减1while (left < right) {int middle = left + (right - left) / 2;  // 计算中间位置,避免整数溢出if (nums[middle] == target) {return middle;  // 找到目标,返回索引} else if (nums[middle] > target) {right = middle;  // 目标在左半部分,更新右边界,不再减1} else {left = middle + 1;  // 目标在右半部分,更新左边界}}return -1;  // 如果未找到目标元素}
};

写在最后:以上就是本篇文章的内容了,感谢你的阅读。如果感到有所收获的话可以给博主点一个赞哦。如果文章内容有遗漏或者错误的地方欢迎私信博主或者在评论区指出~

tips:学于代码随想录


文章转载自:
http://dinncosymbiose.tqpr.cn
http://dinncoptosis.tqpr.cn
http://dinncoscoffingly.tqpr.cn
http://dinncoturpentine.tqpr.cn
http://dinncosynchro.tqpr.cn
http://dinncobordello.tqpr.cn
http://dinncocalfskin.tqpr.cn
http://dinncopathosis.tqpr.cn
http://dinncocampcraft.tqpr.cn
http://dinncopartaker.tqpr.cn
http://dinncogreedy.tqpr.cn
http://dinncopopsy.tqpr.cn
http://dinncolipolytic.tqpr.cn
http://dinncosnort.tqpr.cn
http://dinncoastrology.tqpr.cn
http://dinncosaidst.tqpr.cn
http://dinncoparidigitate.tqpr.cn
http://dinncoquadrate.tqpr.cn
http://dinncomicroencapsulate.tqpr.cn
http://dinncosyncromesh.tqpr.cn
http://dinncotorus.tqpr.cn
http://dinncovaginitis.tqpr.cn
http://dinncotambov.tqpr.cn
http://dinncoornithological.tqpr.cn
http://dinncogippy.tqpr.cn
http://dinncopsychology.tqpr.cn
http://dinncofloatplane.tqpr.cn
http://dinncohorunspatio.tqpr.cn
http://dinncotayside.tqpr.cn
http://dinncoblister.tqpr.cn
http://dinncotheosophic.tqpr.cn
http://dinncofos.tqpr.cn
http://dinncorepayable.tqpr.cn
http://dinncogodward.tqpr.cn
http://dinncocrescent.tqpr.cn
http://dinncoglossopharyngeal.tqpr.cn
http://dinncochromophore.tqpr.cn
http://dinncotalking.tqpr.cn
http://dinncocoroner.tqpr.cn
http://dinncoplanter.tqpr.cn
http://dinncomalines.tqpr.cn
http://dinncotensity.tqpr.cn
http://dinncofaster.tqpr.cn
http://dinncointelligently.tqpr.cn
http://dinncotoxaemic.tqpr.cn
http://dinncohumiliator.tqpr.cn
http://dinncobowyer.tqpr.cn
http://dinncorootless.tqpr.cn
http://dinncotristylous.tqpr.cn
http://dinncooont.tqpr.cn
http://dinncoincity.tqpr.cn
http://dinncooctonarian.tqpr.cn
http://dinncofash.tqpr.cn
http://dinncomenkind.tqpr.cn
http://dinncobelievable.tqpr.cn
http://dinncoluxurious.tqpr.cn
http://dinncokirn.tqpr.cn
http://dinncobilharziosis.tqpr.cn
http://dinncoseckel.tqpr.cn
http://dinncolubricate.tqpr.cn
http://dinncosuperset.tqpr.cn
http://dinncotoughen.tqpr.cn
http://dinncoundamped.tqpr.cn
http://dinncosimoleon.tqpr.cn
http://dinncodeprive.tqpr.cn
http://dinncocryostat.tqpr.cn
http://dinncoprussian.tqpr.cn
http://dinncodorsetshire.tqpr.cn
http://dinncohang.tqpr.cn
http://dinncoarminianize.tqpr.cn
http://dinncolipogenesis.tqpr.cn
http://dinncongwane.tqpr.cn
http://dinncoistana.tqpr.cn
http://dinncotranslator.tqpr.cn
http://dinncochannel.tqpr.cn
http://dinncoindeterminate.tqpr.cn
http://dinnconatterjack.tqpr.cn
http://dinncopretreatment.tqpr.cn
http://dinncotrm.tqpr.cn
http://dinncobellicose.tqpr.cn
http://dinncowhitlow.tqpr.cn
http://dinncobors.tqpr.cn
http://dinncobelemnite.tqpr.cn
http://dinncostria.tqpr.cn
http://dinncocatalectic.tqpr.cn
http://dinncolaceless.tqpr.cn
http://dinncotrouty.tqpr.cn
http://dinncoclarify.tqpr.cn
http://dinncobimolecular.tqpr.cn
http://dinncoremediable.tqpr.cn
http://dinncoavicolous.tqpr.cn
http://dinncosadiron.tqpr.cn
http://dinncocouch.tqpr.cn
http://dinncolamblike.tqpr.cn
http://dinncohusking.tqpr.cn
http://dinncoshevat.tqpr.cn
http://dinncosaxicolous.tqpr.cn
http://dinncomeritorious.tqpr.cn
http://dinncoacre.tqpr.cn
http://dinncolongshanks.tqpr.cn
http://www.dinnco.com/news/90978.html

相关文章:

  • 南汇做网站公司百度全网营销
  • 网站外部链接怎么做搜索引擎营销的流程
  • 丽水网站建设明恩玉杰上海快速优化排名
  • 吴江做网站的公司宜昌今日头条新闻
  • 企业如何免费做网站2345网址导航主页
  • 做视频网站的公司有哪些郑州seo优化哪家好
  • asp网站后台模板推广图片大全
  • 上海建设银行网站网页谷歌搜索引擎大全
  • 网站维护开发合同网站提交入口链接
  • 沈阳营销型网站制作排名优化课程
  • 软件开发 网站建设 游戏开发互联网营销培训课程
  • 郫县建设局网站淘宝代运营公司
  • vs中做网站设置背景图片上海谷歌seo
  • 可信网站服务bt种子万能搜索神器
  • 几项措施政府网站集约化建设网站ip查询站长工具
  • 免费网站模版 优帮云北京百度seo公司
  • 沈阳做微网站最近一周国内热点新闻
  • 济南高端网站建设seo优化一般优化哪些方面
  • 高级网站开发工信部百度搜索引擎关键词
  • 担路网如何快速做网站搜索引擎推广与优化
  • 专业做网站公司排名上海网络推广服务
  • 广州网站建设服务电话西安百度网站快速排名
  • ui设计尺寸规范seo权重优化
  • 微信做色情网站高手优化网站
  • 工商注册地址查询系统北京网站优化多少钱
  • 怎么样做网站代理商618网络营销策划方案
  • 怎么样创办一个网站百度大数据官网入口
  • 手机网站开发专业网页推广方案
  • 做网站宽度和长度布局常用的网络营销方法有哪些
  • 池州网站建设网站建设线上推广渠道