【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的升序排列中第 ...
随机推荐
- Docker: Unknown – Unable to query docker version: x509: certificate is valid for
I was playing around with Docker locally and somehow ended up with this error when I tried to list ...
- java class 文件
class 文件是什么 1).是对一个java类或一个java接口的全面描述:2).是对java程序二进制文件格式的精确定义:3).一个class文件中只能包含一个类或接口:4).作为程序传给jvm的 ...
- PPT模板中的”书签”
引言 在项目中生成文档报告经常需要word中,其中的关键就是书签,通过定位和替换书签中的值来达到生成定制的报告(详见Word模板中的表格处理):但在PPT中却没有书签这个概念,所以,不能采用这种方式. ...
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解 一.Jquery遍历筛选数组 1.jquery grep()筛选遍历数组 $().ready( function(){ v ...
- JAVA 线程池入门事例
线程池这个概念已经深入人心了,今天就是通过几个入门事例,学习一下线程池在JAVA中的应用. 一.大小固定的线程池——Executors.newFixedThreadPool() 下面咱们明确两个类: ...
- LNMP一键安装包-CentOS/Ubuntu/Debian自动安装Nginx,MySQL,PHP
适用环境: 系统支持:CentOS.Ubuntu.Debian 内存要求:≥128M 安装了什么: 1.Nginx-1.2.1 2.MySQL 5.5.25 3.PHP 5.2.17或PHP 5.3. ...
- vuejs组件交互 - 03 - vuex状态管理实现组件交互
组件交互模式的使用场景 简单应用直接使用props down,event up的模式就可以了 小型应用使用事件中心模式即可 中大型应用使用vuex的状态管理模式 vuex 包含要管理的应用数据和更新数 ...
- 好久不git这么多问题
本来想把本地项目上传GitHub一下,打开gitbash, git init 之前配置过了 用户名和邮箱以及ssh等 $ git remote add origin https://github.co ...
- Percona-XtraBackup系列二:备份恢复
#在备份较大数据量的时候推荐xtrabackup,这个工具比mysqldump要快很多. 一.Xtrabackup介绍 1,Xtrabackup是什么 Xtrabackup是一个对InnoDB做数据备 ...
- cmake交叉编译android(转)
生成cmake编译所需的文件 #-H指向CMakeLists.txt文件父级目录 #-B指向中间产物目录 #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY指向so输出目录 #-DCM ...