【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的升序排列中第 ...
随机推荐
- ASP入门(五)- VBScript过程和函数
VBScript过程 被封装在Sub和End Sub语句之中的一系列语句 不具有返回值 可带参数 我们的SubFunction.asp中展示了Sub的用法,代码如下: <% Sub mySub( ...
- ecshop中smarty最常用的6大基本语法
模板制作比较核心的其实就是变量的使用,因为这些就是数据的根本,想从数据库里获取数据并显示在界面就必须使用smarty来展示,下面是关于smarty的介绍和使用.Smarty是一个php模板引擎,它分开 ...
- C#.NET常见问题(FAQ)-如何使用2D绘图控件ZedGraph绘制坐标轴和坐标曲线
添加数据:示例添加了一条sin曲线和一条cos曲线,注意cos曲线比sin曲线点更密集(可以用这种方式控制点的采样疏密程度) 默认显示效果如下图所示,可以框选一个部分看放大效果 右击某个点可以 ...
- Redis分布式锁实现秒杀业务(乐观锁、悲观锁)
https://blog.csdn.net/lmb55/article/details/78266905
- scala中Option和Some
Option的解释: Represents optional values. Instances of Option are either an instance of scala.Some or t ...
- vSphere Data Protection – a new backup product included with vSphere 5.1
August 27, 2012 By Vladan SEGET This new backup product replaces VMware Data Recovery, which has bee ...
- SqlServer和Mysql插入记录前判断是否存在,存在则插入,不存在则修改。
SqlServer中是这样: ) ,@title,@searchKeys,@serviceIntervalSecond,@sleepMillisecondPerSearch) ELSE UPDATE ...
- python获取命令行参数 启动文件
import sys # python D:\MARK\temp.py m1 start 9090 print(sys.argv) # ['D:\\MARK\\temp.py', 'm1', 'sta ...
- 使用paramiko中 Server not found in known_hosts的错误解决
报错的格式如下: 主要是缺少set_missing_host_key_policy配置信息. set_missing_host_key_policy方法,是制定连接远程主机没有本地密钥或HostKey ...
- ssh 防止超时掉线
超时掉线的机制原始驱动力是什么?反正远程操作久置掉线确实挺烦的. 解决的办法呢,也是有的,客户端和服务器端都可以做.就是使用 no-op 反空闲协议发送呼吸包,来确认另一端是否在线:没回应就下线,而不 ...