Binary search

C++中的binary_search函数详解 是C++ STL中的一个强大工具,它可以帮助我们在已排序的序列中快速查找元素。然而,使用它时需要注意一些事项,包括确保输入范围已排序,理解其返回值的含义,以及如何使用自定义比较函数。是一个非常有用的函数,它可以在一个已排序的序列中查找一个特定的元素。这个函数的使用非常直观,但是了解其工作原理和一些注意事项可以帮助我们更有效地使用它。它会在输入范围内查找这个值,并返回一个布尔值,表示这个值是否存在。的时间复杂度为O(log n),其中n是输入范围中的元素数量。在这个例子中,我们定义了一个自定义的比较函数。 阅读详情
Binary search
5000(ms)
65535(kb)
1074 / 5460
Tags: 减治法
A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an element halfway with what has been determined to be an element too low in the list and one too high in the list. A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. A binary search is an example of a divide and conquer algorithm (more specifically a decrease and conquer algorithm) and a dichotomic search (more at Search algorithm). The most common application of binary search is to find a specific value in a sorted list. To cast this in the frame of the guessing game (see Example below), realize that we are now guessing the index, or numbered place, of the value in the list. This is useful because, given the index, other data structures will contain associated information. Suppose a data structure containing the classic collection of name, address, telephone number and so forth has been accumulated, and an array is prepared containing the names, numbered from one to N. A query might be: what is the telephone number for a given name X. To answer this the array would be searched and the index (if any) corresponding to that name determined, whereupon it would be used to report the associated telephone number and so forth. Appropriate provision must be made for the name not being in the list (typically by returning an index value of zero), indeed the question of interest might be only whether X is in the list or not. If the list of names is in sorted order, a binary search will find a given name with far fewer probes than the simple procedure of probing each name in the list, one after the other in a linear search, and the procedure is much simpler than organising a hash table though that would be faster still, typically averaging just over one probe. This applies for a uniform distribution of search items but if it is known that some few items are much more likely to be sought for than the majority then a linear search with the list ordered so that the most popular items are first may do better. The binary search begins by comparing the sought value X to the value in the middle of the list; because the values are sorted, it is clear whether the sought value would belong before or after that middle value, and the search then continues through the correct half in the same way. Only the sign of the difference is inspected: there is no attempt at an interpolation search based on the size of the differences. Your task is to write a program that, given a set numbers of ascending and a key, finding a particular postion in a sorted list.
输入
The input contains one total numbers(N<=5000000) and a find key,followed by a line containing the integer numbers ascending sets. 
输出
if find the key in the sorted list, output containing postion in a sorted list, else ouput -1.
样例输入
10 7
0 1 2 3 4 5 6 7 8 9
样例输出
8


#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;

int func(int t) {
    int x=1;
    for(int i=0;i<t;i++)
        x*=2;
    return x;
}

int main() {
    int x,y,flag=-1;
    scanf("%d %d",&x,&y);
    int *a = new int [x];
    for(int i=0;i<x;i++){
        scanf("%d",&a[i]);
        if(a[i]==y)
            flag=i+1;
    }
    cout<<flag<<endl;
}

C++ STL中的Binary search cout<<b<<endl; int c=binary_search(a,a+9,40);//查找失败,返回0 cout<<c<<endl; int d=lower_bound(a,a+9,10)-a; cout<<d<<endl; int e=lower_bound(a,a+9,101)-a; cout<<e<<endl; int f=upper_bound(a,a+9,10)-a; cout<<f 阅读详情

相关推荐

BinarySearch

NULL 博文链接:https://enria.iteye.com/blog/1441105

Binary Search(二分搜索)

转载请注明出处 leonchen1024.com/2018/08/14/… 二分搜索(binary search),也叫做 折半搜索(half-interval search),对数搜索(logarithmic search),对半搜索(binary chop),是一种在有序数组中查找某一特定元素的搜索算法. 二分搜索有几个变体.特别是,分散层叠(fractional cascading)(将每个...

weixin_34233618的博客 990

elementui中el-select和el-tree实现下拉树形多选功能

elementui中el-select和el-tree实现下拉树形多选功能

weixin_49203377的博客 6559

二分查找(Binary Search)

二分查找

weixin_45847849的博客 2114

Kylin-Desktop-V10-SP1-General-Release-2303-X86_64-海光版(hygon c86)安装与配置指南

本文提供了在国产海光C86处理器平台上安装与配置Kylin Desktop V10 SP1操作系统的详细指南。内容涵盖从下载专用镜像、制作启动盘、BIOS关键设置、磁盘分区方案,到系统安装后的驱动优化、必备软件安装及常见故障排除,旨在帮助用户顺利完成hygon c86平台的系统部署与优化。

tree的博客 1394

华为昇腾服务器npu中安装paddle,pytorch等环境,训练代码迁移适配等问题记录

参考文档:https://github.com/PaddlePaddle/PaddleCustomDevice/blob/develop/backends/npu/README_cn.md

qq_29220369的博客 4115

二分查找(Binary Search)及常见的具体实现(C++和Python)

二分查找(Binary Search)及常见的具体实现方式

cnds123的专栏 1643

开源DIY | 打造会“听”会“走”的智能移动垃圾桶

本文详细介绍了如何利用Arduino和超声波传感器打造一款会“听”会“走”的智能移动垃圾桶。通过开源硬件和简单编程,实现声控移动和自动避障功能,适合创客爱好者和家庭DIY。项目成本控制在200元内,涵盖硬件搭建、电路连接和核心功能实现,是入门级机器人制作的理想选择。

weixin_33670713的博客 234

简单实用算法——二分查找法(BinarySearch)

算法概述# 二分查找(英语:binary search),也叫折半查找(英语:half-interval search),是一种在有序数组中查找特定元素的搜索算法。所以,二分查找的前提是数组必须是有序的。 时间复杂度、空间复杂度请参照下图(图片来自wikipedia): 适用情况# 二分查找只适用顺序存储结构。为保持表的有vb.net教程序性,在顺序结构里插入和删除都必须移动大量的结点。因此,二分查找特别适用于那种一经建立就很少改动、而又经常需要查找的线性表。 对那些查找少而又经常需要改动的线性表,可采用

chinaherolts2008的博客 4650

数控系统低配使用的病根与解法:从二次开发到宝元SDK实战

数控系统是工业制造的核心算力,但多数工厂只用到其基础功能,存在典型的“低配使用”现象。设备选型只看硬件参数、工艺编程习惯保守、管理层只保生产不挖潜能,导致系统内置的高精轮廓控制、刀具寿命管理、数据采集接口等能力长期闲置。实际上,现代数控系统本质是带开放接口的工业计算设备,通过二次开发SDK(如宝元数控的scif2_bcb.dll)即可将设备状态、坐标、报警等数据实时接入MES,实现设备联网与透明化生产。从功能解锁、参数调优到数据采集,再到自动化集成,是一条可复用的满配路径。本文结合工程实践,解析低配使用的根

weixin_29194255的博客 226

帆软报表JS导出避坑指南:如何正确处理sessionid和参数传递

本文深入探讨了帆软报表中通过JavaScript实现一键导出的关键技术,重点解析了如何正确获取并传递sessionid以确保会话不丢失,以及如何构造包含必要参数的导出URL。文章详细对比了预览后导出与直接URL导出两种方式的差异,并提供了从参数拼接、前端触发下载到高级场景(如SPA集成)的完整避坑指南,帮助开发者构建稳定可靠的自定义导出功能。

weixin_42522857的博客 164

【算法】二分搜索

本文对二分搜索算法进行了详细地介绍,包括概述、代码实现和应用。

代码星辰的博客 5720

【2026最新】3分钟Windows极速部署OpenClaw,完整可复制命令+全链路避坑指南

很多新手在Windows环境部署OpenClaw时,总会遇到各种玄学问题:WSL环境配置失败、Docker镜像拉取超时、容器启动后无法访问、权限报错、数据丢失……网上大量教程要么是Linux环境的,要么是早已过时的旧版本配置,照着操作不仅没法3分钟部署,反而要花几小时踩坑填坑。

2601_94871597的博客 495

C++中的Binary_search

之前遇到二分的问题,都会手动实现,如果稍有不慎就会在返回值或者是区间的开闭问题上犯错,在这里记录一下c++STL库中的函数,在一般情况下可以使用来提高效率。但二分的本质还是要掌握的。 常用操作: 头文件: #include 使用方法: 1.binary_search:查找某个元素是否出现 a。函数模板: binary_search(arr[],arr[]+size,indx) b.参数说明: ar...

小飞猪的博客 9412

Android功耗系列专题理论之六:外设功耗问题分析方法

本文介绍了移动设备功耗问题的分析方法,重点针对传感器和GPU两大模块。传感器部分详细讲解了通过ADB命令获取日志、状态差异分析和单传感器隔离测试的排查方法,并给出优化建议。GPU部分从基础功耗测量、场景测试到功能模块拆解进行多维度分析。同时阐述了GNSS/GPS功耗的测试方法,包括前后台对比、星座双频拆解及SAP/DPO等特殊功能的电流分析,为移动设备功耗优化提供系统性的诊断思路。

377

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

binary_search浅析

binary_search浅析 一、介绍 1、binary_search是c++ < algorithm >库中的一个函数。 2、常用于判断某个元素是否存在,若元素存在则返回值为1,不存在则返回值为0。 二、用法 1、函数原型: bool binary_search(beg,end,const T& value,op) 或 bool binary_search(beg,end,const T& value) 2、函数的引用: binary_search(查找首地址,查找终止

m0_52073315的博客 1208

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

stl binary_search

今天调程序的时候,犯了一个错误。 使用binary_search的时候,没有先把vector sort一下,看了下c++ reference手册的描述 binary_search的实现大致如下: template bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value )

lambda_tea的专栏 755

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

binary_search

/**binary_search 实现二分查找,如果是随机存取迭代器,则是对数复杂度,否则为线性复杂度。 * 要求目标区间已排序。 */ /** template bool binary_search (ForwardIterator first, ForwardIterator last, const T& val);

人生代码,代码人生。。。 1066

binary_search函数

1.功能:查找指定元素是否存在 2.函数原型: bool binary_search(iterator beg, iterator end, value); 查找指定的元素,找到返回true,否则返回false 注意:不适用于无序序列 beg 开始迭代器 end 结束迭代器 value 查找的元素 #include<iostream> #include<vector> #include<string> #include<algorithm&g...

sang_12345的博客 847

[leetcode]95,96 Unique Binary Search Trees I&II

详解 public int numTrees(int n) { int [] G = new int[n+1]; G[0] = G[1] = 1; for(int i=2; i<=n; ++i) { for(int j=1; j<=i; ++j) { G[i] += G[j-1] * G[i-j]; } } ...

Belle_Chou的博客 151

binary search

在使用binary search 的时候要先对内表进行排序

SAP方砖的专栏 550

算法之旅,直奔<algorithm>之四 binary_search

binary_search(vs2010) 引言 binary_search是我学习总结的第四篇,这是查找非常厉害的算法,也是非常基础的。 作用 binary_search的作用是在容器中检测容器中是否存在给定值的元素,如果存在则返回true,否则返回 false。 在使用的时候有两个重载,分别如下 template bool bi

cqs_2012 1276

理论第八课——binary_search

二分法检索(binary_search)又称折半检索,二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组中,首先将给定值key与字典中间位置上元素的关键码(key)比较,如果相等,则检索成功;这样,经过一次比较就缩小一半的检索区间,如此进行下去,直到检索成功或检索失败。二分法检索是一种效率较高的检索方法,要求字典在顺序表中按关键码排序。否则,若key小,则在字典前半部分中继续进行二分法检索;若key大,则在字典后半部分中继续进行二分法检索。这个函数可搭配理论第六课二分查找函数使用;

zyxtjzys的博客 283

POJ1054 枚举【STL__binary_search()_的应用】

①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, data to search) 1 #include <stdio.h> 2 #include <string.h> 3 #include <mat...

weixin_30713953的博客 160

LeetCode 95. Unique Binary Search Trees II

Description https://leetcode.com/problems/unique-binary-search-trees-ii/ 给定n,生成所有结构上唯一的二叉搜索树,并返回所有这些树的root。 Solving Ideas 与Unique Binary Search Trees类似,不同的是,这里需要构造出所有结构上唯一的BST 实现起来比较简单就是使用 递归 的方法,遍历所有...

wylu 318
上一篇: The Josephus Problem
下一篇: 变位词
CGD_ProgramLife
博客等级 码龄9年 11粉丝 64原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值