BinarySearch,逆序排列的数组的二分查找(折半查找),C++非递归+递归实现
1 // To Compile and Run: g++ binary_search.cc -std=c++11 -Wall -O3 && ./a.out 8
2
3
4 #include <stdlib.h>
5 #include <assert.h>
6
7 #include <iostream>
8 #include <vector>
9
10
11 int BinarySearch(const std::vector<int> &inArr, int target) {
12 int left = 0;
13 int right = inArr.size() - 1;
14
15 while (true) {
16 if (left > right) {
17 std::cout << "Failed to find: " << target << "\n";
18 return -1;
19 }
20
21 int mid = left + (right - left) / 2;
22 if (target == inArr[mid]) {
23 return mid;
24 }
25
26 if (target > inArr[mid]) {
27 right = mid - 1;
28 }
29 // target < inArr[mid]
30 else {
31 left = mid + 1;
32 }
33 }
34
35 std::cerr << "Could never reach here.\n";
36 return -1;
37 }
38
39 int main(int argc, char const *argv[]) {
40 const std::vector<int> arr {
41 8, 7, 4, 3, -5, -9
42 };
43 assert(argc >= 2);
44 int target = atoi(argv[1]);
45
46 int indexOfTarget = BinarySearch(arr, target);
47 if (indexOfTarget < 0) {
48 std::cout << "Failed to find: " << target << "\n";
49 return -1;
50 }
51 std::cout << "Find: " << target << " in " << indexOfTarget << "\n";
52
53 return 0;
54 }
对于顺序排列的数组,只修改26行的符号就行了。
使用递归实现,只需要在BinarySearch函数中去掉while循环,判断结束后,return自身就行了。
代码如下:
1 // To Compile and Run: g++ binary_search.cc -std=c++11 -Wall -O3 && ./a.out 8
2
3
4 #include <stdlib.h>
5 #include <assert.h>
6
7 #include <iostream>
8 #include <vector>
9
10
11 int BinarySearchRecursive(const std::vector<int> &inArr, int target, int left, int right) {
12 if (left > right) {
13 std::cout << "Failed to find: " << target << "\n";
14 return -1;
15 }
16
17 int mid = left + (right - left) / 2;
18 if (target == inArr[mid]) {
19 return mid;
20 }
21
22 if (target > inArr[mid]) {
23 right = mid - 1;
24 }
25 else {
26 left = mid + 1;
27 }
28
29 return BinarySearchRecursive(inArr, target, left, right);
30 }
31
32 int main(int argc, char const *argv[]) {
33 const std::vector<int> arr {
34 8, 7, 4, 3, -5, -9
35 };
36 assert(argc >= 2);
37 int target = atoi(argv[1]);
38
39 int indexOfTarget = BinarySearchRecursive(arr, target, 0, arr.size() - 1);
40 if (indexOfTarget < 0) {
41 std::cout << "Failed to find: " << target << "\n";
42 return -1;
43 }
44 std::cout << "Find: " << target << " in " << indexOfTarget << "\n";
45
46 return 0;
47 }
BinarySearch,逆序排列的数组的二分查找(折半查找),C++非递归+递归实现的更多相关文章
- Java数组逆序排列
//逆序排列原理 /* A: 数组逆序原理* a: 题目分析* 通过观察发现,本题目要实现原数组元素倒序存放操作.即原数组存储元素为{12,69,852,25,89,588},逆序后为原数组存储元素变 ...
- Java实现蓝桥杯VIP算法训练 数组逆序排列
试题 算法训练 数组逆序排列 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 编写一个程序,读入一组整数(不超过20个),并把它们保存在一个整型数组中.当用户输入0时,表示输入结束. ...
- C语言 · 逆序排列
算法提高 逆序排列 时间限制:1.0s 内存限制:512.0MB 问题描述 编写一个程序,读入一组整数(不超过20个),并把它们保存在一个整型数组中.当用户输入0时,表示输入结束.然 ...
- 算法笔记_158:算法提高 逆序排列(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 编写一个程序,读入一组整数(不超过20个),并把它们保存在一个整型数组中.当用户输入0时,表示输入结束.然后程序将把这个数组中的值按逆序重新存 ...
- 51nod 1020 逆序排列 DP
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...
- SQL-27 给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。 提示:在sqlite中获取datetime时间对应的年份函数为strftime('%Y', to_date)
题目描述 给出每个员工每年薪水涨幅超过5000的员工编号emp_no.薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列. 提示:在s ...
- SQL-15 查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列
题目描述 查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列CREATE TABLE `employees` (`emp_no ...
- 51nod 1020 逆序排列 递推DP
1020 逆序排列 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么 ...
- 51nod 1020 逆序排列——dp
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...
- 1020 逆序排列(DP)
1020 逆序排列 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序 ...
随机推荐
- Django项目的创建、启动、停止
1. 首先安装Django的包 pip install Django 的命令安装包 2. 配置环境变量,必须配置,后边需要用到django的命令,这个包和别的包不太一样的地方 C:\Users**** ...
- GNOME 窗口添加最大化、最小化按钮
1.安装工具 使用终端命令安装优化工具 yum install gnome-tweak-tool 2.配置 gnome-tweak-tool 安装完毕后,在应用程序的"工具"中找到 ...
- 远程连接linux桌面
https://zhuanlan.zhihu.com/p/127265014 https://zhuanlan.zhihu.com/p/93438433
- import cv2时出现ImportError: DLL load fail:找不到指定模块
- Python 面试题整理
一.语言特性 1.什么是Python?使用Python有什么好处?Python和其他语言的区别? Python是一种编程语言,它有对象,模块,线程,异常处理和自动内存管理. 好处:开源.简洁.简单.方 ...
- Linux profile、bashrc、bash_profile
一.profile 文件 1.profile 文件的作用 profile(/etc/profile),用于设置系统级的环境变量和启动程序,在这个文件下配置会对所有用户生效.当用户登录(login)时, ...
- golang_nethttp
package main import ( "encoding/json" "fmt" "log" "net/http" ...
- Blob下载
下载方式 const aBlob = new Blob( array, options ); export function downLoadFile(data: ArrayBuffer, fileN ...
- go-bindata安装问题
问题描述使用命令 go get -u github.com/jteeuwen/go-bindata/... 报错: go get -u github.com/go-bindata/go-bindata ...
- Web Uploader上传文件
Web Uploader是百度提供的. 1:下载:http://fex.baidu.com/webuploader/(官方下载/示例) 2:使用Web Uploader文件上传需要引入三种资源:JS, ...