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级算法题 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序 ...
随机推荐
- py10函数之嵌套-名称空间作用域
# 函数是第一类对象:函数名指向的值可以被当中参数传递 # 1.函数名可以被传递# name = 'jason'# x = name# print(x)# print(id(x))# def func ...
- Linux FTP服务器配置文件vsftpd.conf 配置
配置文件/etc/vsftpd/vsftpd.conf local_enable=YES write_enable=YESlocal_umask=022dirmessage_en ...
- C# RGB转Brush
C#中自定义一个Brush,使用Color赋RGB值给Brush: dataGrid2.HorizontalGridLinesBrush = new SolidColorBrush(System.Wi ...
- mac os 11 Big Sur 根目录无法写入解决办法
本文目的是解决无法在 / 目录下创建目录的问题: 关闭SIP 重启机器,按住 command + R 选择 磁盘工具 在导航栏 窗口 中打开 终端 ,执行如下命令: csrutil status ## ...
- Linux 常用监控指标总结
1. Linux运维基础采集项 做运维,不怕出问题,怕的是出了问题,抓不到现场,两眼摸黑.所以,依靠强大的监控系统,收集尽可能多的指标,意义重大.但哪些指标才是有意义的呢,本着从实践中来的思想,各位工 ...
- Accelerated molecular dynamics simulation of Silicon Crystals on TaihuLight using OpenACC 阅读
基于OpenACC的太湖之光硅晶体加速分子动力学模拟 2020 摘要:以SW26010异构多核处理器和扩展的编程模型,使用多体势(Tersoff)执行固体共价晶体的分子动力学(MD)模拟. Am ...
- win7下virtualbox虚拟机中安装centos后设置共享文件夹
报错信息: building the main Guest Additions module FAILEDunable to find the sources of your current Linu ...
- Python学习笔记文件读写之遍历目录树
随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...
- js时间戳转换
第一个参数传时间戳,第二个连接符可以看你的心情,我喜欢'-' formatDate(value, spe = '/') { value = value * 1000 //10位数时间戳要乘1000 1 ...
- gym104076H
hehezhou 的鬼才神仙题解根本看不懂好吧. 首先判掉 \(n=1\). 然后考虑最后一步,发现只用考虑怎样的左右端点可能见面. 左右和右左构成双射,算出总量减去自己配自己再除 \(2\) 即可. ...