【Algorithm】二分查找
今天在学习《编程之美》的时候,看到一个二分查找的题目,发现原来我真的不懂二分查找。
二分查找时候注意的事项:
-
在求二分查找的中间点时没有使用
midIndex = (minIndex + maxIndex) / 2
是因为,以免 minIndex + maxIndex之后会导致溢出而出现错误。
- 注意循环的循环终止条件及边界元素的判定。
下面把牛人做的该题部分的扩展(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】二分查找的更多相关文章
- 【algorithm】 二分查找算法
二分查找算法:<维基百科> 在计算机科学中,二分搜索(英语:binary search),也称折半搜索(英语:half-interval search)[1].对数搜索(英语:logari ...
- 【Algorithm】二分查找(递归实现)
二分查找(递归实现),Java 代码如下: public class BinarySearch { public static int rank(int key, int[] a) { return ...
- JAVA源码走读(二)二分查找与Arrays类
给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找 ...
- LA 2678 Subsequence(二分查找)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- CF 600B Queries about less or equal elements --- 二分查找
CF 600B 题目大意:给定n,m,数组a(n个数),数组b(m个数),对每一个数组b中的元素,求数组a中小于等于数组该元素的个数. 解题思路:对数组a进行排序,然后对每一个元素b[i],在数组a中 ...
- 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 ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
- C. Tavas and Karafs 二分查找+贪心
C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...
- K-th Number 线段树(归并树)+二分查找
K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...
随机推荐
- KDiff
BeyondCompare是收费的,用了一段时间不能用了.找到一个 KDiff做对比工具,也很好用. 在这里下载: http://sourceforge.net/projects/kdiff3/fil ...
- Auty 2017——WebMonitor接口本地检测平台
转载:http://www.cnblogs.com/LanTianYou/p/6272484.html#_label0_0 目录 2016Auty诞生 2017一个新的开始 WebMonitor接口本 ...
- CSS3实现的苹果网站搜索框效果
在线演示 本地下载 用CSS3相关属性生成的动态搜索框效果.
- JavaScript 之 JavaScript 对象
重新看JavaScript对象,参考资料: RUNOOB.COM:http://www.runoob.com/jsref/jsref-fromcharcode.html: CodePlayer:htt ...
- PYQT实现简单的浏览器功能
主要的类 QMainWindow 提供一个有菜单条.锚接窗口(例如工具条)和一个状态条的主应用程序窗口. http://www.kuqin.com/qtdocument/qmainwindow.htm ...
- mysqld.exe
mysqld.exe是mysql的服务端程序,开启之后才能使用mysql.exe 将mysql安装成服务很简单: mysqld.exe install mysql 删除服务也很简单: sc delet ...
- JVM类加载机制与对象的生命周期
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6536048.html 虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最 ...
- Linux安装nginx并设置https(openssl
一.安装依赖包 1.$sudo apt-get install openssl 或者$sudo apt-get install libssl-dev 2.$sudo apt-get instal ...
- Android File Transfer
about a question in developing.. When you run app and created a file of db,but Android File Transfer ...
- 〖Linux〗Kubuntu文件管理器单例的设置(即:一个工作区只一个文件管理器)
有没有一种,情况: 1. 程序A打开了文件管理器: 2. 程序B又打开了文件管理器: 导致开了两个文件管理器,太不舒服了: 搜索下 kubuntu dolphin single instance,果然 ...