双向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]
    
  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*搜索算法

你可能感兴趣的:(双向a*搜索算法_双向搜索算法)