当前位置:网站首页>二分查找
二分查找
2022-06-12 21:01:00 【又秃又弱】
二分查找(折半查找)
算法效率分析:时间复杂度、空间复杂度
平均时间复杂度:O(n);
空间复杂度:O(1);
前提:数组元素有序
代码实现:
1,main函数内实现:
int main()
{
int array[MAX] = { 1,2,3,5,6,7,8,9,10,11,12 };
int length = sizeof(array) / sizeof(array[0])-1;//减1!!
int right = array[length];
int left = 0;//int left = array[0]不对,我们现在要的是下标不是下标对应的值
int num;//num是要判断的值
scanf("%d",&num);
int MidIndex;
while(left<=right)
{
MidIndex = (left + right)/2;
if (array[MidIndex] == num)//不是数组下标的比较MidIndex == num
{
printf("该数已找到!");
break;
}
else if (array[MidIndex] < num)
{
left = MidIndex + 1;
}
else
{
right = MidIndex - 1;
}
if (right >= left)
{
printf("该数不存在");
break;
}
}
}
2,函数封装(递归实现)
int BinarySearch0(int *arr,int length,int left,int right,int value)
{
if (left > right)
return -1;//不存在该数据
int mid = ((right - left) >> 1 + left); //位运算优化
if (arr[mid] == value)
return mid;
if (arr[mid] > value)//往小的那边即左边走
return BinarySearch0(int* arr, int length, left, mid - 1, value);
else//往大的那边即右边走
return BinarySearch0(int* arr, int length, mid+1, left, value);
}
void BinarySearch(int *arr,int length,int value){
int left = 0, right = len - 1;
//封装范围
int index = BinarySearch0(arr, length, left, right,value);
return index;
}
int main() {
int arr[] = { 1,2,3,4,5 };
int len = strlen(arr);
BinarySearch(arr, len);
return 0;
}
边栏推荐
- Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
- What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
- 在同花顺开户安全么,买股票怎么网上开户
- Compréhension préliminaire des expressions régulières cognitives (regex)
- leetcode:207. 课程表
- Properties to YML
- Algorinote_ 2_ Main theorem and Akra bazzi theorem
- In the spring recruitment of 2022, the test engineer will have a full set of interview strategies to thoroughly understand all the technical stacks (all dry goods)
- Can flush open an account? Can you directly open the security of securities companies on the app? How to open an account online when buying stocks
- leetcode:210. 課程錶 II
猜你喜欢
JSP中的监听器
作用域和作用域链
没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?
What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
入行5年从10k的功能测试到年薪40w的测试开发,花7天时间整理的超全学习路线
Nexus3 build local warehouse
Data visualization - biaxial comparison effect
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
String Basics
Shell language
随机推荐
Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
Properties to YML
MinIO客户端(mc命令)实现数据迁移
Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot
Summary of machine learning materials
Shell language
Introduction to scala basic grammar (III) various operators in Scala
在同花顺开户安全么,买股票怎么网上开户
HR SaaS unicorn is about to emerge. Will the employee experience be the next explosive point?
Understanding of functions
#981 Time Based Key-Value Store
JS中如何实现重载
How can CTCM in the inspection lot system status of SAP QM be eliminated?
It has been engaged in the functional test of 10K to the test development of 40W annual salary for 5 years, and spent 7 days sorting out the super comprehensive learning route
torch. unique()
#981 Time Based Key-Value Store
没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?
Lintcode:127. Topology sorting
SAP QM preliminary - cannot assign a sampling policy to an inspection characteristic when maintaining an inspection plan by executing transaction code qp02?
Transaction code qs28 of SAP QM preliminary level