今天在学习《编程之美》的时候,看到一个二分查找的题目,发现原来我真的不懂二分查找。

  二分查找时候注意的事项:

  1.   

    在求二分查找的中间点时没有使用

    midIndex = (minIndex + maxIndex) / 2

    是因为,以免 minIndex + maxIndex之后会导致溢出而出现错误。

  2. 注意循环的循环终止条件及边界元素的判定。

下面把牛人做的该题部分的扩展(C++)拿来展示一下,以供学习:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std; /*
//1: 给定一个有序(不降序)数组arr,求最大的i使得arr[i]等于v,不存在则返回-1
int bisearch(char arr[][10], int begin, int end, char *v)
{
int minIndex = begin;
int maxIndex = end;
int midIndex; while(minIndex < maxIndex - 1) {
midIndex = minIndex + (maxIndex - minIndex) / 2; if(strcmp(arr[midIndex], v) <= 0)
minIndex = midIndex;
else
maxIndex = midIndex;
} //从最大索引开始判断
if(!strcmp(arr[maxIndex], v))
return maxIndex;
else if(!strcmp(arr[minIndex], v))
return minIndex;
else
return -1;
}
*/ /*
//2: 一个有序(不降序)数组arr,求任意一个i使得arr[i]等于v,不存在则返回-1
int bisearch(char (*arr)[10], int begin, int end, char *v)
{
int minIndex = begin;
int maxIndex = end;
int midIndex;
while(minIndex < maxIndex) { midIndex = minIndex + (maxIndex - minIndex) / 2; if(strcmp(*(arr + midIndex), v) < 0)
minIndex = midIndex + 1;
else if(strcmp(*(arr + midIndex), v) > 0)
maxIndex = midIndex - 1;
else
return midIndex;
} cout << "minIndex = " << minIndex << " maxIndex = " << maxIndex << endl; if(!strcmp(*(arr + minIndex), v))
return minIndex; return -1;
}
*/ /*
//3:给定一个有序(不降序)数组arr,求最小的i使得arr[i]等于v,不存在则返回-1
int bisearch(char (*arr)[10], int begin, int end, char *v)
{
int minIndex = begin;
int maxIndex = end;
int midIndex; while(minIndex < maxIndex - 1) {
midIndex = minIndex + (maxIndex - minIndex) / 2;
if(strcmp(*(arr + midIndex), v) < 0)
minIndex = midIndex;
else
maxIndex = midIndex;
} cout << "minIndex = " << minIndex << " maxIndex = " << maxIndex << endl; //从最小数开始判断
if(!strcmp(*(arr + minIndex), v))
return minIndex;
else if(!strcmp(*(arr + maxIndex), v))
return maxIndex;
else
return -1;
}
*/ /*
//4:给定一个有序(不降序)数组arr,求最大的i使得arr[i]小于v,不存在则返回-1
int bisearch(char (*arr)[10], int begin, int end, char *v)
{
int minIndex = begin;
int maxIndex = end;
int midIndex; while(minIndex < maxIndex - 1) {
midIndex = minIndex + (maxIndex - minIndex) / 2;
if(strcmp(*(arr + midIndex), v) < 0)
minIndex = midIndex;
else
maxIndex = midIndex;
} //从最大数开始判断
if(strcmp(*(arr + maxIndex), v) < 0)
return maxIndex;
else if(strcmp(*(arr + minIndex), v) < 0)
return minIndex;
else
return -1;
}
*/ //5; 给定一个有序(不降序)数组arr,求最小的i使得arr[i]大于v,不存在则返回-1
int bisearch(char (*arr)[], int begin, int end, char *v)
{
int minIndex = begin;
int maxIndex = end;
int midIndex; while(minIndex < maxIndex - ) {
midIndex = minIndex + (maxIndex - minIndex) / ; if(strcmp(*(arr + midIndex), v) <= )
minIndex = midIndex;
else
maxIndex = midIndex;
} //从小数开始判断
if(strcmp(*(arr + minIndex), v) > )
return minIndex;
else if(strcmp(*(arr + maxIndex), v) > )
return maxIndex;
else
return -;
} int main()
{
char a[][] = {"abc", "bcd", "bddaaa", "ddcd", "ddd", "ddd", "ddd", "ddd", "xxx", "xxxx"};
char v[] = "dddd";
int last = sizeof(a) / (sizeof(char) * );
int index = bisearch(a, , last-, v);
printf("index of v is %d\n", index); return ;
}

参考资料

  [1]http://blog.csdn.net/caryaliu/article/details/8134041

【Algorithm】二分查找的更多相关文章

  1. 【algorithm】 二分查找算法

    二分查找算法:<维基百科> 在计算机科学中,二分搜索(英语:binary search),也称折半搜索(英语:half-interval search)[1].对数搜索(英语:logari ...

  2. 【Algorithm】二分查找(递归实现)

    二分查找(递归实现),Java 代码如下: public class BinarySearch { public static int rank(int key, int[] a) { return ...

  3. JAVA源码走读(二)二分查找与Arrays类

    给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找 ...

  4. LA 2678 Subsequence(二分查找)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. CF 600B Queries about less or equal elements --- 二分查找

    CF 600B 题目大意:给定n,m,数组a(n个数),数组b(m个数),对每一个数组b中的元素,求数组a中小于等于数组该元素的个数. 解题思路:对数组a进行排序,然后对每一个元素b[i],在数组a中 ...

  6. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

  8. C. Tavas and Karafs 二分查找+贪心

    C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...

  9. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

随机推荐

  1. 虚拟机Linux下解决ping时出现 unknown host问题

    在虚拟机中使用CentOS6.5时,ping www.baidu.com出现报错信息:“ping: unknown hostwww.baidu.com”,虚拟机和物理机网络连接是NAT方式,物理机访问 ...

  2. android中RecyclerView控件的使用

    1.RecyclerView控件不在标准的库里面,需要先引入,引入比较简单,点击控件右边的下载按钮即可 2.先添加一个新闻实体类,用来为新闻列表提供数据,news.java: package com. ...

  3. 【iOS地图开发】巧妙打造中英文全球地图

    地图开发的同学们经常遇到这样的问题,国内版地图开发,用高德或者百度就行了.但是,国外的地图怎么办?这里告诉大家,如果利用iOS地图,打造中英文的,国内国外都能用的,全球地图. 制作全英文地图的展示并不 ...

  4. Mac os 系统头像位置。

    ~/Library/Containers/com.apple.ImageKit.RecentPictureService/Data/Library/Images/Recent Pictures/

  5. fread/fwrite

    fread/fwrite第二个参数和第三个参数的区别以及与返回值的关系 size_t fwrite_unlocked(const void * __restrict ptr, size_t size, ...

  6. 创建一个入门的JAVA WEB站点(REST JERSEY)

    最近一直在看TOMCAT,想要自己创建一个小WEB站点,有不想要部署在其他的容器内这是一个不错的学习对象. 一.选择合适的模版 mvn archetype:generate -DarchetypeCa ...

  7. mahout基于Hadoop的CF代码分析(转)

    来自:http://www.codesky.net/article/201206/171862.html mahout的taste框架是协同过滤算法的实现.它支持DataModel,如文件.数据库.N ...

  8. @Service注解的使用

    首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...

  9. VB数组的清除

    在一个程序中,同一数组只能用Dim语句定义一次.但有时可能需要清除数组的内容或对数组重新定义,这可以用:Erase语句来实现. 格式:Erase(数组名)[,(数组名)] 功能:用于重新初始化静态数组 ...

  10. px 和 em 的区别

    相同点:px和em都是长度单位: 异同点:px的值是固定的,指定是多少就是多少,计算比较容易.em得值不是固定的,并且em会继承父级元素的字体大小.浏览器的默认字体高都是16px.所以未经调整的浏览器 ...