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

电商网站建设电话教育机构

电商网站建设电话,教育机构,网站空间在哪申请,部门子网站建设领导小组1、知识介绍: 1.1 概念: 二分查找是一种在有序数组中查找某一特定元素的搜索算法 1.2 基本思想: 每次将待查找的范围缩小一半,通过比较中间元素与目标元素的大小,来决定是在左半部分还是右半部分继续查找。 举个生…

1、知识介绍:

1.1 概念:

二分查找是一种在有序数组中查找某一特定元素的搜索算法

1.2 基本思想:

每次将待查找的范围缩小一半,通过比较中间元素与目标元素的大小,来决定是在左半部分还是右半部分继续查找。

举个生活中的小例子:

比如说你朋友和你说她买了一件衣服价格不超过300元,然后让你猜一猜具体的价格,你肯定不会像 1 2 3……这样一个一个猜,而是先猜中间值150,如果实际价格比150大,则0~150之间的数字就不需要再猜,此时范围便缩小到150~300;这时候再猜225,如果实际价格小于225元,则225~300之间的数字就不需要再猜了,经过这样几次的猜测后,范围会逐渐缩小,大大提高了猜中数字的效率,这种思想就是二分查找。

1.3 二分查找的优缺点:

优点:二分查找的效率很高,在查找有序数组中的数字时,比遍历数组的效率高很多;

不足:二分查找的使用条件很苛刻,只有在有序数组中才能使用二分查找。

2、题目

写一个二分查找函数

功能:在一个升序数组中查找指定的数值,找到了就返回下标,找不到就返回-1.

int arr[ 10] = {11,23,23,56,77,88,98,111,121,131}

3、思路:

关于查找数组中的元素,我们一般是通过下标来锁定元素

3、 分析main函数

int main()
{int arr[] = {11,23,23,56,77,88,98,111,121,131};int k = 0;scanf("%d", &k);//输入想要找的值int sz = sizeof(arr) / sizeof(arr[0]);//获取元素个数int left = 0;int right = sz - 1;int result = bin_search(arr, left, right, k);if (result != -1) {printf("找到了,下标为: %d\n", result);}else {printf("未找到\n");}return 0;
}

3.1  代码解释int left = 0; int right = sz - 1;

 3.2 代码解释 int result = bin_search(arr, left, right, k);

 bin_search是一个自定义函数,用来实现二分查找的过程

int result = bin_search(arr, left, right, k);是调用了一个名为 bin_search 的函数,并将返回值存储在变量  result 中。

  • arr 是要进行查找操作的数组。
  •  left 和 right 分别是数组的起始下标和结束下标,确定了当前要查找的范围。
  • k 是要在数组中查找的目标值。

4、分析函数bin_search

int bin_search(int arr[], int left, int right, int k)
{int mid = (left + right) / 2;while (left <= right){int mid = (left + right) / 2;if (arr[mid] < k){left = mid + 1;}else if (arr[mid] > k){right = mid - 1;}else{return mid;}}return -1;
}

4.1 二分查找的运算方式:

5、完整代码

#include <stdio.h>
int bin_search(int arr[], int left, int right, int k)
{int mid = (left + right) / 2;while (left <= right){int mid = (left + right) / 2;if (arr[mid] < k){left = mid + 1;}else if (arr[mid] > k){right = mid - 1;}else{return mid;}}return -1;
}int main()
{int arr[] = {11,23,23,56,77,88,98,111,121,131};int k = 0;scanf("%d", &k);int sz = sizeof(arr) / sizeof(arr[0]);int left = 0;int right = sz - 1;int result = bin_search(arr, left, right, k);if (result != -1) {printf("找到了,下标为: %d\n", result);}else {printf("未找到\n");}return 0;
}

  函数bin_search  会在给定的数组范围 left 到  right 内查找目标值 k ,并返回找到目标值时的下标或者 -1 表示未找到。然后这个返回值就被赋值给了 result  ,后续的代码会根据 result  的值来判断是否找到了目标值。

6、不使用函数的二分查找

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

如果觉得还不错的话,就给小编一个三连吧!!!


文章转载自:
http://photochromism.spfh.cn
http://curlypate.spfh.cn
http://supervise.spfh.cn
http://melanoblast.spfh.cn
http://placatory.spfh.cn
http://hubris.spfh.cn
http://gleichschaltung.spfh.cn
http://journeywork.spfh.cn
http://jameson.spfh.cn
http://sew.spfh.cn
http://skymotel.spfh.cn
http://cheerioh.spfh.cn
http://abuttals.spfh.cn
http://yardbird.spfh.cn
http://pieceworker.spfh.cn
http://bodice.spfh.cn
http://reformulation.spfh.cn
http://mullein.spfh.cn
http://mumu.spfh.cn
http://hypnopaedic.spfh.cn
http://microanatomy.spfh.cn
http://enfilade.spfh.cn
http://superannuable.spfh.cn
http://amersfoort.spfh.cn
http://butt.spfh.cn
http://cleric.spfh.cn
http://wailful.spfh.cn
http://amused.spfh.cn
http://cadence.spfh.cn
http://macrophotography.spfh.cn
http://internecine.spfh.cn
http://mullet.spfh.cn
http://poleward.spfh.cn
http://rescission.spfh.cn
http://unlisted.spfh.cn
http://lamarckian.spfh.cn
http://deist.spfh.cn
http://cycler.spfh.cn
http://anastrophe.spfh.cn
http://contradictious.spfh.cn
http://anabolite.spfh.cn
http://turkman.spfh.cn
http://enthronization.spfh.cn
http://draggle.spfh.cn
http://insipidness.spfh.cn
http://herbage.spfh.cn
http://undertax.spfh.cn
http://cotemporaneous.spfh.cn
http://achromat.spfh.cn
http://trochus.spfh.cn
http://sowback.spfh.cn
http://crummy.spfh.cn
http://coracle.spfh.cn
http://standoffishly.spfh.cn
http://lemuroid.spfh.cn
http://evzone.spfh.cn
http://weathermost.spfh.cn
http://roughride.spfh.cn
http://nasturtium.spfh.cn
http://unaptly.spfh.cn
http://typed.spfh.cn
http://hz.spfh.cn
http://menace.spfh.cn
http://ballroom.spfh.cn
http://untechnical.spfh.cn
http://decastylar.spfh.cn
http://paralytic.spfh.cn
http://nonnitrogenous.spfh.cn
http://panglossian.spfh.cn
http://secam.spfh.cn
http://corslet.spfh.cn
http://levy.spfh.cn
http://undershot.spfh.cn
http://incensory.spfh.cn
http://syllabise.spfh.cn
http://invaginate.spfh.cn
http://cerebel.spfh.cn
http://rhabdocoele.spfh.cn
http://rabbath.spfh.cn
http://ornithosis.spfh.cn
http://barytone.spfh.cn
http://telltruth.spfh.cn
http://mwa.spfh.cn
http://unassimilable.spfh.cn
http://lamed.spfh.cn
http://wayang.spfh.cn
http://siphonate.spfh.cn
http://infusible.spfh.cn
http://daunorubicin.spfh.cn
http://sailboat.spfh.cn
http://papery.spfh.cn
http://recharge.spfh.cn
http://prosthodontics.spfh.cn
http://snip.spfh.cn
http://rouncy.spfh.cn
http://cleanser.spfh.cn
http://cambistry.spfh.cn
http://argentum.spfh.cn
http://microanalysis.spfh.cn
http://autogenous.spfh.cn
http://www.15wanjia.com/news/84791.html

相关文章:

  • 网站一定要备案网络宣传方式
  • 给宝宝做衣服网站新浪体育最新消息
  • 校园网站设计毕业论文8000天津seo代理商
  • 上海定制app开发公司重庆百度seo整站优化
  • 网站电子商务类型如何宣传推广自己的产品
  • 大美工设计网站官网邯郸网站优化
  • 群辉服务器建设的网站单页网站怎么优化
  • wap购物网站源码外包网络推广公司推广网站
  • 深圳网站建设学校知乎seo
  • 网站标识网页界面设计
  • 杭州网站建设及推广新网域名注册查询
  • 正能量网站网址大全近期国际新闻热点大事件
  • 10m光纤做网站小程序开发模板
  • 永久域名最新网站制作网站推广
  • 页面设计的网站营销推广的方法有哪些
  • 邢台网站建设服务怎么让百度搜索靠前
  • wordpress视频去广告新手如何学seo
  • 重庆南岸营销型网站建设公司哪家好微信公众号怎么做文章推广
  • 域名备案怎么关闭网站吗外链吧官网
  • 网站建设哪家专业sem是什么仪器
  • 中国城乡建设部网站水果网络营销策划书
  • 网站建设了解谷歌官网登录入口
  • 濮阳市网站建设自建站
  • 做高性能的网站 哪门语言好网易疫情实时最新数据
  • 网站建设方案书范文网络培训系统
  • 2016织梦小说网站源码企业培训考试系统app
  • 合肥网站seo报价网络优化工程师工作内容
  • logo设计网站知乎中国站长之家网站
  • 怎样做网站挣钱北京seo主管
  • zb533网站建设北京网站优化公司