折半查找

递归的折半查找算法 问题一:递归的折半查找算法 注意:折半查找有一个条件,数据必须是有顺序性的,不然折半查找毫无意义; 原问题:有一数组A[10],里面存放了十个整数,顺序递增。任意输入一个n(位于数组里外均可),如果n数与数组,则显示n位于数组中的位置,如果n不属于数组,则显示错误;要求用递归的方法实现折半查找; 扩展:适用于输入所有顺序递增数据实现折半查找。 实验代码如下: #include <stdio.... 阅读详情

折半查找:

了解并掌握数组的折半查找法

import java.util.Scanner;

public class HalfSearch {
    public static void main(String[]args){
        int search;
        Scanner d=new Scanner(System.in);
        System.out.print("请输入你所要查的数:");
        search=d.nextInt();
        int a[]={23,35,96,102,110,116,123};

        int left=0;
        int right=a.length-1;
        int mid=3;
        while(left<=right){
            mid=(right+left)/2;
            int s=a[mid];
            if(s==search){
            break;
            }
            else if(s<search){
                left=mid+1;
            }
            else{
                right=mid-1;
            }
        }
        if(left<=right){
            System.out.println(search+"在数组中第"+(mid+1)+"位");
            }
        else{
            System.out.println("里面没有这个元素");
        }
        }
    }

运行结果:

这里写图片描述

【数据结构】折半查找 主要内容 基本概念 折半查找(Binary Search) 基本概念 在介绍查找算法前我们先重温几个重要概念: 1)数据:客观事物的符号表示,是所有能输入到计算机中,并能被计算机程序处理的符号的总称。如数学计算中用到的整数和实数,文本编辑中用到的字符串,多媒体程序处理的图形、图像、声音及动画等通过特殊编码定义后的数据。 2)数据元素:数据的基本单位,在计算机中通常作为一个整体进行考... 阅读详情

相关推荐

强化学习_06_pytorch-TD3实践(BipedalWalkerHardcore-v3)

DDPG的critic会高估, 从而导致actor策略失败。TD3是增加了三个关键技巧优化DDPG。经过优化后的TD3(Twin Dalayed DDPG 双延迟深度确定性策略梯度算法)适合于具有高维连续动作空间的任务。

RL小站 2689

4-13 折半查找

给一个严格递增数列,函数int binSearch(SeqList T, KeyType k)用来二分地查找k在数列中的位置。 函数接口定义: int binSearch(SeqList T, KeyType k) 其中T是有序表,k是查找的值。 裁判测试程序样例: #include using namespace std; #define MAXLEN 50 typedef

许愿 2848

项目方案:社会视频资源整合接入汇聚系统解决方案(五)

视频监控接入和视频汇聚平台,形成整合平台,平台服务层包括编码设备、存储设备、解码设备等的接入服务,提供基础视频服务、传输服务、存储服务、控制服务及事件服务。两种不同的接入方式:监控设备接入,是指接入社会单位视频监控系统的DVR(数字硬盘录像机)、DVS和IPC、DVR/NVR、CVR等设备;平台对接,是指对接接入社会单位数字视频监控平台。AS-VAG视频监控接入网关支持onvif、国标等接入,也支持多种SDK接入方式,包括国产的主流品牌支持海康、大华、宇视、华为等SDK,国际品牌支持松下和axis等SDK。

weixin_70208651的博客 2464

简单的折半查找算法(Java)

折半查找的优点是能够最大减少查询的时间复杂度,但也对查询的目标数组或集合提出一定要求,即数组或集合中的数字排序必须是有序的,否则不能用折半查找,只能用遍历。以下是简单的折半查找算法: public class test { public static void main(String[] args) { int arr[]={11,15,18,19,21,25,28,29,35}; ...

qq_42698983的博客 2770

告别瞎猜!用通达信L2资金流向函数,手把手教你识别主力动向(附实战公式)

本文详细解析通达信L2资金流向函数,手把手教你构建主力动向监控系统。通过分档数据解析、委买委卖攻防解读及实战公式应用,帮助投资者精准识别主力建仓与虚假拉升信号,提升股票交易决策效率。

weixin_26908317的博客 2074

PSCAD学习笔记(1)Project Settings(重点snapshot设置)

若不勾选,则横坐标为实际时间2-7s,故障显示在第3s发生。图片中时间坐标轴显示为0-5s,故障时刻显示为第1s,即0-1s展示系统稳态,1-5s展示系统发生故障后的暂态响应,如何通过快照文件实现?勾选该项后,使用快照文件启动仿真会将仿真起始时间强制设置为0s,反之不勾选则依据快照文件保存的时间轴继续(即坐标为实际时间)。如果已有的快照文件保存的是0s至5s的仿真内容,则仿真运行时间从5s开始,到10s结束。如果已有的快照文件保存的是0s至2s的仿真内容,则仿真运行时间从2s开始,到7s结束;

gwenh的博客 7342

折半查找的实现代码:

递归实现: #include <iostream> // 二分法:递归 int searchBin(int arr[], int x, int low, int high) { int mid; if(low > high) return -1; mid = (low + high) / 2; if(x == arr[mid]) return mid; ...

ZhangJiqun&191cm&85kg 6614

Pinocchio 结合 CasADi 进行 IK 逆运动学及 Mujoco 仿真

摘要:本视频演示了如何利用Pinocchio和CasADi实现机械臂逆运动学(IK)求解,并在Mujoco中进行仿真。通过构建优化问题(最小化位姿误差),结合IPOPT求解器对机械臂关节角度进行优化。代码支持导入URDF/MJCF模型,并以so100机械臂为例,实现了末端执行器位置跟踪的仿真测试。关键步骤包括:建立平移/旋转误差函数、设置约束条件、调用CasADi求解,最终在Mujoco中实时更新关节状态完成运动控制。

darcsdn 1002

折半查找的原理及其java的两种实现

折半查找的原理及其java的两种实现

mine_song的博客 2429

避免踩坑!杰理AC63蓝牙名修改常见问题及解决方案(含BLE后缀处理)

本文详细解析了杰理AC63蓝牙设备名称修改的全过程,包括基础操作、BLE后缀处理及高级调试技巧。针对蓝牙名修改中的常见问题,提供了实用的解决方案和代码示例,帮助开发者避免踩坑并实现高效定制。

q6r7s8t9的博客 410

ISO26262功能安全--产品开发过程

目录 1.总体理解 2.概念阶段——吹牛,我们是打草稿的 2.1 相关项定义 2.2 HARA分析 2.3 功能安全目标 2.4 安全需求与安全概念 3. 系统阶段——闭门造神车,我们开始修炼 3.1 技术安全需求(TSC) 3.2 系统架构设计 3.3 安全分析与独立性分析 3.4 软硬件接口(HSI) 4. 硬件阶段——苦其心志,苦练经骨 4.1 硬件架构...

AgingMoon的博客 1万+

数据结构之折半查找

折半查找又称二分查找,它仅仅适用于有序的顺表。

chuyu的博客 3万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【查找】折半查找

折半查找 如果从文件中读取的数据记录的关键字是有序排列的(递增的或是递减的),则可以用一种更有效率的查找方法来查找文件中的记录,这就是折半查找法,又称为二分搜索。 折半查找的基本思想:减少查找序列的长度,分而治之地进行关键字的查找。他的查找过程是:先确定待查找记录的所在的范围,然后逐渐缩小查找的范围,直至找到该记录为止(也可能查找失败)。例如文件记录的关键字序列为:     (1,3,5,7,9,...

秃头仔仔-安凯雯的博客 4万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

C语言——折半查找法

查找数据位序——折半查找法

m0_69438595的博客 3万+

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

折半查找判定树——(快速判断某棵树是否为折半查找判定树)

折半查找        也被称作二分查找,即将需要查找的元素与数组中间的元素进行比较;若比中间的元素小,则再与前子表的中间元素进行比较,以此类推直至查找到所需查找元素,或者所需查找元素不在此表中。 折半查找判定树(此树必为平衡树)        即由折半查找过程中所产生的树,首尾除以二取整。 下面主要介绍如何快速判断树是否为折半查找判定树   &nb

叫我蘑菇先生 2万+

折半查找——对比次数

【问题描述】 折半查找的基本思想为: (1)在有序表中,取中间记录作为比较对象,若给定值与中间记录的关键码相等,则查找成功; (2)若给定值小于中间记录的关键码,则在中间记录的左半区继续查找; (3)若给定值大于中间记录的关键码,则在中间记录的右半区继续查找; (4)不断重复上述过程,直到查找成功或失败。 已知一个顺序表中的数据都是整型且有序的,给出一个待查找值,请设计算法计算出采用折半查找该数值...

weixin_44034024的博客 1万+

实现折半查找的算法

/** *    实验题目: *        实现折半查找的算法 *    实验目的: *        领会折半查找的过程和算法设计 *    实验内容: *        设计程序,输出在顺序表(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)中采用 *    折半查找方法查找关键字9的过程 * */ #include &lt;stdio.h&gt; #define MAX_...

xiezhi123456的博客 6762

经典算法之折半查找

经典算法之折半查找

weixin_68153081的博客 7367

折半查找法

折半查找   折半查找,也称二分查找,在某些情况下相比于顺序查找,使用折半查找算法的效率更高。但是该算法的使用的前提是静态查找表中的数据必须是有序的。例如,在{2,10,16,19,23,44,56,58,69,80,99} 这个查找表使用折半查找算法查找数据之前,需要首先对该表中的数据按照所查的关键字(从小到大)进行排序:{2,10,16,19,23,44,56,58,69,80,99}。   ...

duan179的博客 2万+

二维数组的折半查找

二维数组的折半查找

2301_81871776的博客 1818

折半查找python实现

折半查找python实现 折半查找是常用的查找方法(在按大小顺序排列中的数组或者列表中更是如此),与传统的顺序查找相比,它查找的效率更高。 算法思想 算法的思想很直接,也就是先把第一个和最后一个作为作为low和high的值,然后将要查找的数字与这两个位置对应的数值进行比较(当然,为了保证算法稳健性,先判断要查找的数字在不在数组或列表中,如果不存在则提示重新输入)。 算法操作步骤 设给定的数组为A,...

如切如磋 9308

折半查找判定树

具有n个结点的折半查找树的高度为 给定一棵树,该树的ASLsucc=(1+22+34+4*4)/11=33/11=3

weixin_45655152的博客 5645
上一篇: 判断闰年
下一篇: Java一维数组简单操作
Labigale
博客等级 码龄9年 6粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值