【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的升序排列中第 ...
随机推荐
- 有用的java工具
1.Jsoup html页面解析 2.FastJson java中json处理工具,类似于gson 3.jodd 类似于apache commons的一些常用工具集 4.Selenium IDE we ...
- [海蜘蛛] 海蜘蛛 V8 全线无限试用版 免费发布破解教程
http://bbs.p52.cn/forum.php?mod=viewthread&tid=3499&extra=page%3D1&page=1&_dsign=79c ...
- 在CentOS 7上安装Nginx服务器
下面我就我在CentOS上安装Nginx经验做简单的记录,以备后查. 1.下载nginx-release包 以CentOS 7为例,下载nginx软件包:http://nginx.org/packag ...
- input[type="checkbox"]与label对齐
项目中遇到文字与 checkbook 无法水平对齐, 源码如下: <div align='center'> <input type="checkbox" id=& ...
- 微信小程序 - loading(组件)
更新日期: 2019/3/8:首次发布 2019/3/12:增加loadOpacity透明度控制,默认0.5. 以及修改居中方式 Loading 参数: 1. type:loading(必需参数) 2 ...
- 微信小程序 - 支持html空格(提示)
仅限于text标签,decode参数:官方api.
- Go语言中Path包用法
// path package main import ( "fmt" "os" "path" "path/filepath&qu ...
- did not call through to super.onCreate()
android.util.SuperNotCalledException: Activity {com.xkyiliao.xkhospital/com.xkyiliao.xkhospital.acti ...
- unique-paths I &II 路径数,动态规划
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 怎样使用Fiddler获取WebApi的token值?
User-Agent: Fiddler Host: localhost: Content-Length: Content-Type: application/json grant_type=passw ...