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<right) //如果写成left<=right 有可能越界 { int mid=left+(right-left)/2; if (a[mid]>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<right //所以当为【)不要把未判断元素直接做right的下标 } else if(a[mid]<key) { left=mid+1; } else { return a+mid; } } return NULL; } void testBinary() { int a[10]={0,1,3,5,45,78,98,111,121,454}; for (int i=9;i>=0;i--) { int *temp=BinarySearch(a,10,i); if(temp==NULL) { cout<<"NULL"<<endl; } else cout<<*temp<<endl; } } //递归版本 int * BinarySearch_R(int *a,int n,int key,int left,int right) { if(a==NULL||n==0) { return NULL; } if(left<right) { int mid=left+(right-left)/2; if(a[mid]>key) { return BinarySearch_R(a,n,key,left,mid); } else if(a[mid]<key) { return BinarySearch_R(a,n,key,mid+1,right); } else { return a+mid; } } else { return NULL; } } void testBinary_R() { int a[10]={0,1,3,5,45,78,98,111,121,454}; for (int i=9;i>=0;i--) { int *temp=BinarySearch_R(a,10,i,0,10); if(temp==NULL) { cout<<"NULL"<<endl; } else cout<<*temp<<endl; } }
(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]<key) { left=mid+1; //a[mid]已经判断过了 } else return a+mid; } return NULL; } void testBinary() { int a[10]={0,1,3,5,45,78,98,111,121,454}; for (int i=9;i>=0;i--) { int *temp=BinarySearch_C(a,10,i); if(temp==NULL) { cout<<"NULL"<<endl; } else cout<<*temp<<endl; } } //递归版本 int * BinarySearch_CR(int *a,int n,int key,int left,int right) { if(a==NULL||n==0) { return NULL; } if(left<=right) { int mid=left+(right-left)/2; if(a[mid]>key) { return BinarySearch_R(a,n,key,left,mid-1); } else if(a[mid]<key) { return BinarySearch_R(a,n,key,mid+1,right); } else { return a+mid; } } else { return NULL; } } void testBinary_R() { int a[10]={0,1,3,5,45,78,98,111,121,454}; for (int i=9;i>=0;i--) { int *temp=BinarySearch_CR(a,10,i,0,9); if(temp==NULL) { cout<<"NULL"<<endl; } else cout<<*temp<<endl; } }
题目:
正整数数组a[0], a[1], a[2], ···, a[n-1],n可以很大,大到1000000000以上,但是数组中每个数都不超过100。现在要你求所有数的和。假设这些数已经全部读入内存,因而不用考虑读取的时间。希望你用最快的方法得到答案。
提示:二分。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站标题:有关二分查找的边界思考-创新互联
本文URL:https://www.cdcxhl.com/article4/ddoooe.html
成都网站建设公司_创新互联,为您提供域名注册、网站策划、面包屑导航、小程序开发、响应式网站、品牌网站设计
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联