博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 搜索 二进制_C中的二进制搜索
阅读量:2509 次
发布时间:2019-05-11

本文共 1642 字,大约阅读时间需要 5 分钟。

c# 搜索 二进制

Here you will get program for binary search in C.

在这里,您将获得用C进行二进制搜索的程序。

Binary search algorithm can be applied on a sorted array to search an element. Search begins with comparing middle element of array to target element. If both are equal then position of element is returned. If target element is less than middle element of array then upper half of array is discarded and again search continued by dividing the lower half. If target element is greater than middle element then lower half is discarded and search is continued in upper half.

二进制搜索算法可以应用于排序后的数组以搜索元素。 搜索开始于将数组的中间元素与目标元素进行比较。 如果两者相等,则返回元素的位置。 如果目标元素小于数组的中间元素,则丢弃数组的上半部分,并通过划分下半部分再次继续搜索。 如果目标元素大于中间元素,则将下半部分丢弃,并在上半部分继续搜索。

Binary Search in C

Worst Case Time Complexity: O(log n)

最坏情况下的时间复杂度: O(log n)

Best Case Time Complexity: O(1)

最佳情况下时间复杂度: O(1)

Also Read: 

另请阅读:

C语言二进制搜索程序 (Program for Binary Search in C)

Below program shows the implementation of binary search algorithm in C.

下面的程序显示了C语言中二进制搜索算法的实现。

#include
 int main(){    int arr[50],i,n,x,flag=0,first,last,mid;     printf("Enter size of array:");    scanf("%d",&n);    printf("\nEnter array element(ascending order)\n");     for(i=0;i
arr[mid])                first=mid+1;            else                last=mid-1;    }     if(flag==1)        printf("\nElement found at position %d",mid+1);    else        printf("\nElement not found");     return 0;}

Output

输出量

Enter size of array:6

输入数组大小:6

Enter array element(ascending order) 20 27 40 50 58 99

输入数组元素(升序) 20 27 40 50 58 99

Enter the element to search:27

输入要搜索的元素:27

Element found at position 2

在位置2找到元素

影片教学 (Video Tutorial)

翻译自:

c# 搜索 二进制

转载地址:http://bmggb.baihongyu.com/

你可能感兴趣的文章
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>