第五周-上机-10-04-二分搜索
作者:互联网
#include<iostream>
using namespace std;
int bs(int a[],int num)
{
int i=0,j=9;
while(i<=j)
{
int mid=(i+j)/2;
if(a[mid]=num) return mid;
else if(a[mid]>num) j=mid-1;
else i=mid+1;
}
return -1;
}
int bsc(int a[],int num,int i,int j)
{
int mid=(i+j)/2;
if(i>j) return -1;
else if (a[mid]=num) return mid;
else if (a[mid>num]) return bsc(a,num,i,mid-1)
else return return bsc(a,num,mid+1,j)
}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int num=2;
cout << bs(a,num) << endl;
cout << bsc(a,num,0,8)<< endl;
}
标签:10,return,04,上机,int,mid,else,num,bsc 来源: https://blog.csdn.net/weixin_44568816/article/details/120602817