目录
二分搜索
程序1
输出
Program2
首页 后端开发 C++ 如何使用二分查找算法在C语言中找到数组中的最小元素?

如何使用二分查找算法在C语言中找到数组中的最小元素?

Aug 25, 2023 pm 08:37 PM
c语言 二分查找 最小元素

C编程语言提供了两种搜索技术。它们如下所示:

  • 线性搜索
  • 二分搜索

二分搜索

  • 这种方法只适用于有序列表。
  • 给定列表被分成两个相等的部分。
  • 给定的关键字与列表的中间元素进行比较。

在这里,可能会发生三种情况,如下所示:

  • 如果中间元素与关键字匹配,则搜索将在此成功结束

  • 如果中间元素大于关键字,则搜索将在左侧分区进行。

  • 如果中间元素小于关键字,则搜索将在右侧分区进行。

输入 (i/p) - 未排序的元素列表,关键字。

输出 (o/p) -

  • 成功 - 如果找到关键字
  • 失败 - 否则

如何使用二分查找算法在C语言中找到数组中的最小元素?

key = 20
mid = (low +high) /2
登录后复制

如何使用二分查找算法在C语言中找到数组中的最小元素?

程序1

以下是使用二分查找在数组中找到最小元素的C程序:

#include<stdio.h>
int main(){
   int a[50], n, i, key, flag = 0, low, mid, high;
   printf("enter the no: of elements:");
   scanf ("%d",&n);
   printf("enter the elements:");
   for(i=0; i<n; i++)
      scanf( "%d", &a[i]);
   printf("enter a key element:");
   scanf ("%d", &key);
   low = 0;
   high = n-1;
   while (low<= high ){
      mid = (low + high) /2;
      if (a[mid] == key){
         flag = 1;
         break;
      }
      else{
         if (a[mid] > key)
            high = mid-1;
         else
            low = mid+1;
      }
   }
   if (flag == 1)
      printf ("search is successful");
   else
      printf("search is unsuccessful");
   return 0;
}
登录后复制

输出

当上述程序被执行时,它产生以下结果 −

Run 1:
enter the no: of elements:5
enter the elements:
12
34
11
56
67
enter a key element:45
search is unsuccessful
Run 2:
enter the no: of elements:3
enter the elements:
12
34
56
enter a key element:34
search is successful
登录后复制

Program2

给定下面的C程序,通过使用二分查找来找到数组中的最小元素−

#include<stdio.h>
void Bmin(int *a, int i, int n){
   int j, temp;
   temp = a[i];
   j = 2 * i;
   while (j <= n){
      if (j < n && a[j+1] > a[j])
         j = j + 1;
      if (temp < a[j])
         break;
      else if (temp >= a[j]){
         a[j / 2] = a[j];
         j = 2 * j;
      }
   }
   a[j/2] = temp;
   return;
}
int binarysearchmin(int *a,int n){
   int i;
   for(i = n/2; i >= 1; i--){
      Bmin(a,i,n);
   }
   return a[1];
}
int main(){
   int n, i, x, min;
   int a[20];
   printf("Enter no of elements in an array</p><p>");
   scanf("%d", &n);
   printf("</p><p>Enter %d elements: ", n);
   for (i = 1; i <= n; i++){
      scanf("%d", &a[i]);
   }
   min = binarysearchmin(a, n);
   printf("\minimum element in an array is : %d", min);
   return 0;
}
登录后复制

输出

当上述程序被执行时,它产生以下结果 −

Enter no of elements in an array
5
Enter 5 elements:
12
23
34
45
56
minimum element in an array is: 12
登录后复制

以上是如何使用二分查找算法在C语言中找到数组中的最小元素?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

c语言中typedef struct的用法 c语言中typedef struct的用法 May 09, 2024 am 10:15 AM

c语言中typedef struct的用法

c语言中strcpy和strcat的区别 c语言中strcpy和strcat的区别 May 08, 2024 pm 01:03 PM

c语言中strcpy和strcat的区别

c语言中real是什么意思 c语言中real是什么意思 May 09, 2024 pm 12:06 PM

c语言中real是什么意思

C语言乘方函数如何实现 C语言乘方函数如何实现 May 09, 2024 pm 11:33 PM

C语言乘方函数如何实现

c语言中scanf出现错误怎么办 c语言中scanf出现错误怎么办 May 09, 2024 am 11:39 AM

c语言中scanf出现错误怎么办

_complex在c语言中的用法 _complex在c语言中的用法 May 08, 2024 pm 01:27 PM

_complex在c语言中的用法

restrict在c语言中的用法 restrict在c语言中的用法 May 08, 2024 pm 01:30 PM

restrict在c语言中的用法

_bool在c语言中是什么意思 _bool在c语言中是什么意思 May 08, 2024 pm 01:33 PM

_bool在c语言中是什么意思

See all articles