[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
Hellow,我是jack,今天给大家分享的是C语言实现二分查找。话不多说,让我们直接进入正题。
首先让我们了解下什么是二分查找法
基本思想是将n个元素分成大致相等的两部分,取a [n/2]与x做比较,如果x=a [n/2],则找到x,算法中止;如果x<a
[n/2],则只要在数组a的左半部分继续搜索x,如果x>a [n/2],则只要在数组a的右半部搜索x. 时间复杂度即是while循环的次数。 折半查找法
也称为二分查找法,它充分利用了元素间的次序关系,采用分治策略,可在最坏的情况下用O (log n)完成搜索任务。
知道了这些,就然我们试着用代码来实现一下吧。
void is_found(int a[], int k, int sz) {//二分查找实现方法函数 int left
=0;//定义一个变量指向数组的第一个值 int right = sz - 1;//定义一个变量指向数组的最后一个值 while (left<right) {
//左边的数大于右边的数循环终止 int mid = (left + right) / 2;//设置一个变量并传入中间值 if (a[mid] > k)
{//进行对中间值的判断,如果大于寻找值,则将right指向mid-1 right = mid-1; } else if (a[mid] < k)
{//进行对中间值的判断,如果小于寻找值,则将lift指向mid+1 left = mid + 1; } else { printf("找到了是:
%d\n", mid); break; } } if (left > right) { printf("找不到了"); } }
这是我写的功能函数,接下来然我给大家把代码补充完整。
void is_found(int a[], int k, int sz) {//二分查找实现方法函数 int left
=0;//定义一个变量指向数组的第一个值 int right = sz - 1;//定义一个变量指向数组的最后一个值 while (left<right) {
//左边的数大于右边的数循环终止 int mid = (left + right) / 2;//设置一个变量并传入中间值 if (a[mid] > k)
{//进行对中间值的判断,如果大于寻找值,则将right指向mid-1 right = mid-1; } else if (a[mid] < k)
{//进行对中间值的判断,如果小于寻找值,则将lift指向mid+1 left = mid + 1; } else { printf("找到了是:
%d\n", mid); break; } } if (left > right) { printf("找不到了"); } } int main() {
int arr[] = { 0,1,2,3,4,5,6,7,8,9 }; int k; int sz; sz = sizeof(arr) /
sizeof(arr[0]);//为arr的长度 scanf_s("%d", &k);//输入寻找值 is_found(arr, k, sz);//二分函数
return 0; }
实现的结果为:
大家是否明白了?赶快动手试试吧。
本章就和大家分享到这里,谢谢。
<> <>欢迎指正