双向a*搜索算法_双向搜索算法

双向a*搜索算法

什么是双音搜索? (What is bitonic search?)

Searching a bitonic array is known as bitonic search. An array is said to be bitonic if it has an increasing sequence of integers followed immediately by a decreasing sequence of integers.

搜索双音阵列称为双音搜索 。 如果数组具有递增的整数序列,紧随其后的是递减的整数序列,则称其为双偶数

Given a bitonic array our work is to search a given input element in the bitonic array. In case of minimum time complexity we can think of linear search in O(n) time but bitonic search takes only O(logn) steps to complete the searching.

给定一个双调阵列我们的工作是双调数组中搜索给定的输入元素。 在最小时间复杂度的情况下,我们可以考虑在O(n)时间内进行线性搜索,但双音位搜索仅需O(logn)步骤即可完成搜索。

Let's check the algorithm for bitonic search...

让我们检查一下双音搜索的算法...

Prerequisite: bitonic array of length n, input K

先决条件:长度为n的双音阵列,输入K

Algorithm:

算法:

  1. Set two variable first=0 & last=n-1

    设置两个变量first = 0和last = n-1

  2.     While(first<=last){
    If(first==last)     //array has size 1
    Check whether the only element is Kor not   // takes O(1) time
    Else if (first==last-1)
    Check whether K is one of them or not//takes O(1) time
    Else
    Set a variable mid=first+(last-first)/2;
    If(array[mid]==K)
    Print that element is found & return
    Else if (array[mid]<K)
    Last=mid-1;//as the element is greater than array[mid] we need to
    //reach the increasing sequence
    Else
    First=mid+1; //as the element is less than array[mid] we need to
    //reach the decreasing sequence
    End while loop
    
    
  3. If the element is in the array then the function will return from the loop to main function. If the element is not in the array they the program will come out of the loop and print that the element is missing.

    如果元素在数组中,则函数将从循环返回到主函数。 如果该元素不在数组中,则程序将退出循环并显示该元素丢失。

Discussion:

讨论:

It's clear that we are not searching the whole array. Rather we are partitioning every time based on the value comparison between array[mid] and input, K. Thus it requires O(logn) steps to complete the search.

显然,我们并没有搜索整个数组。 而是每次都基于array [mid]与输入K之间的值比较进行分区。 因此,它需要O(logn)步骤才能完成搜索。

Bitonic搜索的C ++实现 (C++ implementation of the Bitonic Search)

#include<bits/stdc++.h>
using namespace std;
int findelements(int* a, int n,int K){
int first=0,last=n-1,mid;
while(first<=last){
if(first==last){
if(a[first]==K)
return 1;
else
return 0;
}
else if(first==last-1){
if(a[first]==K || a[last]==K)
return 1;
else
return 0;
}
else{
mid=first+(last-first)/2;
if(a[mid]==K)
return 1;
else if(a[mid]<K)
last=mid-1;
else 
first=mid+1;
}
}
return 0;
}
int main(){
int K,count=0,n;
cout<<"enter no of elements\n";        // enter array length
cin>>n;
int* a=(int*)(malloc(sizeof(int)*n));
cout<<"enter elements................\n";  //fill the array
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
cout<<"enter the element to search,K"<<endl;
cin>>K;
if(findelements(a,n,K))
cout<<"element is found\n";
else
cout<<"element not found\n";
return 0;
}

Output (first run)

输出(首次运行)

enter no of elements 
10 
enter elements................ 
1
2
3
4
5
4
3
2
1
0
enter the element to search,K
3
element is found 

Output (second run)

输出(第二次运行)

enter no of elements 
10 
enter elements................ 
1
2
3
4
5
6
5
4
3
2
enter the element to search,K
10 
element not found

翻译自: https://www.includehelp.com/algorithms/bitonic-search.aspx

双向a*搜索算法

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377321.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于LRU缓存简单记录以及代码补全。

目录大概思路时间空间复杂度分析指针操作具体细节代码双向链表设计私有成员变量设计:构造函数和析构函数设计&#xff1a;get与put具体设计双向指针的具体细节添加到头节点函数删除尾节点函数删除节点函数删除节点函数感想今天面试考到LRU&#xff0c;太紧张了&#xff0c;完全…

码农干货系列【4】--图像识别之矩形区域搜索

简介 定位某个图片的矩形区域是非常有用的&#xff0c;这个可以通过手动的选择某个区域来实现定位&#xff0c;图片相关的软件都提供了这个功能&#xff1b;也可以像本篇一个通过程序来实现智能定位。前者会有误差&#xff0c;效率低下&#xff1b;后者选区精度高&#xff0c;效…

算法题复习(回溯)

目录base code棋盘问题51. N 皇后37. 解数独组合问题77. 组合未剪枝优化剪枝优化216. 组合总和 III未剪枝优化剪枝优化17. 电话号码的字母组合39. 组合总和未剪枝优化剪枝优化40. 组合总和 II,挺重要的&#xff0c;涉及到去重了切割问题131. 分割回文串子集问题78. 子集90. 子集…

pfa是什么意思_PFA的完整形式是什么?

pfa是什么意思PFA&#xff1a;预测性故障分析 (PFA: Predictive Failure Analysis) PFA is an abbreviation of Predictive Failure Analysis. It is a technique of a mechanism of the computer that is used to predict impending failures of software or hardware compone…

SqlServer视图(view)

--上节回顾--1.什么是事务--2.事务的特征--原子性、一致性、隔离性、持久性--3.与事务相关的t-sql命令--开启事务&#xff1a;begin transaction--提交事务&#xff1a;commit transaction--回滚事务&#xff1a;rollback transaction ----------------视图-------------------…

Android中的广播Broadcast详解

今天来看一下Android中的广播机制&#xff0c;我们知道广播Broadcast是Android中的四大组件之一&#xff0c;可见他的重要性了&#xff0c;当然它的用途也很大的&#xff0c;比如一些系统的广播&#xff1a;电量低、开机、锁屏等一些操作都会发送一个广播&#xff0c;具体的And…

sql check约束_在SQL中使用CHECK约束

sql check约束Basically, CHECK constraint is used to LIMIT in columns for the range of values. We can use this constraint for single or multiple columns. 基本上&#xff0c; CHECK约束用于限制值范围内的列 。 我们可以将此约束用于单列或多列。 In single column,…

.NET线程池

摘要 深度探索 Microsoft .NET提供的线程池&#xff0c; 揭示什么情况下你需要用线程池以及 .NET框架下的线程池是如何实现的&#xff0c;并告诉你如何去使用线程池。 内容 介绍 .NET中的线程池 线程池中执行的函数 使用定时器 同步对象的执行 异步I/O操作 监视线程池 死锁 有关…

折线分割平面

Input输入数据的第一行是一个整数C,表示测试实例的个数&#xff0c;然后是C 行数据&#xff0c;每行包含一个整数n(0<n<10000),表示折线的数量。Output对于每个测试实例&#xff0c;请输出平面的最大分割数&#xff0c;每个实例的输出占一行。Sample Input2 1 2Sample Ou…

《c++特性》

目录多态构造函数和析构函数存在多态吗&#xff1f;虚函数表虚析构函数纯虚函数和抽象类运行时多态和编译时多态的区别继承设计实例指针对象和普通对象的区别正确初始化派生类方式继承和赋值的兼容规则protected 和 private 继承基类与派生类的指针强制转换如何用C实现C的三大特…

Scala中的while循环

在Scala中的while循环 (while loop in Scala) while loop in Scala is used to run a block of code multiple numbers of time. The number of executions is defined by an entry condition. If this condition is TRUE the code will run otherwise it will not run. Scala中…

Linux操作系统启动过程

在做开发的过程中&#xff0c;突然发现&#xff0c;要对系统做一些有意义的改变&#xff0c;必须要对操作系统的启动过程有一定的了解&#xff0c;不然就是修改你都不知道从哪里下手啊&#xff0c;然后就是找来资料看&#xff0c;去网上看别人的博客&#xff0c;有了前一周一些…

方法命名的区别

GetDecimalFromString ExtractDecimal 这2个方法名那个比较好呢。上边的明显的是中式英语&#xff0c;单词拼凑而成的。下边的更加流畅一些。方法名称取名还是很有要求的。要通俗易懂还要符合文法。从上边的可以扩展出什么想法呢。 ExtractDecimalExtractDoubleExtractInt16Ext…

工作排序问题

Problem statement: 问题陈述&#xff1a; Given an array of jobs where every job has a deadline and a profit. Profit can be earned only if the job is finished before the deadline. It is also given that every job takes a single unit of time, so the minimum p…

牛客网与leetcode刷题(高频题中简单or中等的)

目录1、反转链表2、排序3、先序中序后序遍历4、最小的k个数5、子数组的最大累加和6、 用两个栈实现队列7、142. 环形链表 II8、20. 有效的括号9、最长公共子串(动态规划),磕磕绊绊10、二叉树之字形层序遍历11、重建二叉树12、LRU缓存13、合并两个有序链表15、大数加法16、一个二…

AMUL的完整形式是什么?

AMUL&#xff1a;阿南德牛奶联盟有限公司 (AMUL: Anand Milk Union Limited) AMUL is an abbreviation of Anand Milk Union Limited. It is an Indian milk product cooperative dairy organization that is based in the small town of Anand in the state of Gujarat. AMUL …

mochiweb 源码阅读(十一)

大家好&#xff0c;今天周六&#xff0c;继续接着上一篇&#xff0c;跟大家分享mochiweb源码。上一篇&#xff0c;最后我们看到了mochiweb_socket_server:listen/3函数&#xff1a; listen(Port, Opts, State#mochiweb_socket_server{sslSsl, ssl_optsSslOpts}) ->case moch…

Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能 (转)

转载请注明出处&#xff1a;http://blog.csdn.net/guolin_blog/article/details/9255575 最 近项目中需要用到ListView下拉刷新的功能&#xff0c;一开始想图省事&#xff0c;在网上直接找一个现成的&#xff0c;可是尝试了网上多个版本的下拉刷新之后发现效果都不怎么理 想。有…

Python中的append()和extend()

列出append()方法 (List append() method) append() method is used to insert an element or a list object to the list and length of the List increased by the 1. append()方法用于将元素或列表对象插入列表&#xff0c;并且列表长度增加1。 Syntax: 句法&#xff1a; …

红黑树的实现

目录1、红黑树原理1、红黑树性质2、变换规则&#xff08;从插入结点的角度来讲&#xff09;1.变色2.左旋3.右旋3、删除结点需要注意的地方2、代码1、定义结点以及构造函数2、定义红黑树类以及声明它的方法3、左旋4、右旋5、插入操作6、修正操作7、删除操作3、参考链接1、红黑树…