1、二分查找概念

二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难。因此,折半查找方法适用于不经常变动而查找频繁的有序列表。首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。

2、二分查找简单实现

(1)左开右闭 【 )

//非递归版本int* BinarySearch(int *a,int n,int key){	if (a==NULL||n==0)	{		return NULL;	}	//[)	int left=0;	int right=n;	while(left
key) { right=mid;      //如果写成right=mid+1 元素如果是a[0]=0,a[1]=1,要找1 //left=0,right=1,mid=0 //然后a[mid]<1,right=mid;此时找不到1,因为left
=0;i--) { int *temp=BinarySearch(a,10,i); if(temp==NULL) { cout<<"NULL"<
key)        {            return BinarySearch_R(a,n,key,left,mid);        }        else if(a[mid]
=0;i--) { int *temp=BinarySearch_R(a,10,i,0,10); if(temp==NULL) { cout<<"NULL"<

(2)左闭右闭 【】

int* BinarySearch_C(int *a,int n,int key){    if(a==NULL||n==0)    {        return NULL;    }    //[]    int left=0;    int right=n-1;    while(left<=right)    {        int mid=left+(right-left)/2;        if(a[mid]>key)        {            right=mid-1; //a[mid]的值已经判断过了        }        else if(a[mid]
=0;i--) { int *temp=BinarySearch_C(a,10,i); if(temp==NULL) { cout<<"NULL"<
key)        {            return BinarySearch_R(a,n,key,left,mid-1);        }        else if(a[mid]
=0;i--) { int *temp=BinarySearch_CR(a,10,i,0,9); if(temp==NULL) { cout<<"NULL"<

题目:

正整数数组a[0], a[1], a[2], ···, a[n-1],n可以很大,大到1000000000以上但是数组中每个数都不超过100。现在要你求所有数的和。假设这些数已经全部读入内存,因而不用考虑读取的时间。希望你用最快的方法得到答案。

提示:二分。