Data structure - half search

Algorithm introduction

Binary search, also known as binary search and logarithmic search, is a search algorithm for finding a specific element in an ordered array. The search process starts at the middle element of the array, and ends if the middle element is exactly the element being searched for; if a particular element is greater than or less than the middle element, then searches in the half of the array that is greater than or less than the middle element, and Start the comparison from the middle element as you did at the beginning. If the array is empty at a certain step, it means it cannot be found. This search algorithm reduces the search range by half with each comparison.

Note: The element sequence must be in order, and the half search cannot be used in the case of disorder.

search process

The head and tail of the array a are start and end respectively, then their midpoint elements are subscripted mid(start+end)/2. If a[mid]=target (target element), then directly draw the conclusion that the element exists in the array; if a[mid]<target, it means that the target is in the interval of [mid+1,end]; if a[mid]> target, indicating that the target is located in the interval [start, mid-1]; if start>end, indicating that the search is over and the target does not exist.
This is why the sequence must be ordered. If the sequence is unordered, it will be wrong to judge the interval of the target by the size of a[mid] and target.

Example 1

The array a is {2 4 5 6 8 9 10 12 16 20}, the process of finding element 10 by half search: the
current search subsequence:
2 4 5 6 8 9 10 12 16 20
a[mid]=
8current Searched subsequence:
9 10 12 16 20
a[mid]=12
Current searched subsequence:
9 10
a[mid]=9
Current searched subsequence:
10
a[mid]=10
Element exists

Example 2

The array a is {1 3 4 6 7 8 10 11 13}, the process of searching for element 9 by half search method: the
current searched subsequence:
1 3 4 6 7 8 10 11 13
a[mid]=7
the current searched Subsequence:
8 10 11 13
a[mid]=10
Current searched subsequence:
8
a[mid]=8
The element does not exist

Example 3

The array a is {2 4 6 7 8 9 11 13 14 15 18}, the process of searching for element 8 using the binary search method: the
current search subsequence:
2 4 6 7 8 9 11 13 14 15 18
a[mid]= 9
The subsequence of the current search:
2 4 6 7 8
a[mid]=6
The subsequence of the current search:
7 8
a[mid]=7
The subsequence of the current search:
8
a[mid]=8
The element exists

Example 4

The array a is {2 4 6 7 8 9 11 13 14 15 18}, the process of searching for element 12 by half search method: the
current search subsequence:
2 4 6 7 8 9 11 13 14 15 18
a[mid]= 9
Subsequence currently searched:
11 13 14 15 18
a[mid]=14
Subsequence currently searched:
11 13
a[mid]=11
Subsequence currently searched:
13
a[mid]=13
The element does not exist

time complexity

Because only half of the array's elements are searched each round, the number of elements to search decreases exponentially. The time complexity is O(logN), where log is base 2.

source code

#include<iostream>
#include<stdio.h> 
using namespace std;
int length;
int flag = 0;

void showList(int a[],int start,int end){
    
    
	cout<<"当前搜索的子序列:"<<endl;
	for(int i=start;i<=end;i++){
    
    
		cout<<a[i]<<" ";
	} cout<<"\n";
	
} 

void binary_search(int a[],int start,int end,int target){
    
    
	if(start>end){
    
    //搜索失败
		return ;
	}
	int mid = (start+end)/2;
	if(a[mid]==target){
    
    //查找成功
		flag = 1;
		return ;
	}else if(a[mid]>target){
    
    //中间元素大于目标元素,说明目标元素位置可能在中间元素之前
		binary_search(a,start,mid-1,target);//新的搜索序列为中间位置之前
	}else{
    
    //中间元素小于目标元素,说明目标元素位置可能在中间元素之后
		binary_search(a,mid+1,end,target);//新的搜索序列为中间位置之后
	}
	
}

int main(){
    
    
	int a[20];
	cout<<"请输入元素个数:";
	cin>>length;
	for(int i=0;i<length;i++){
    
    
		cin>>a[i];
	} 
	int target; 
	cout<<"请输入目标元素:";
	cin>>target;
	binary_search(a,0,length-1,target);
	if(flag==1){
    
    
		cout<<"元素存在"<<endl;
	}else{
    
    
		cout<<"元素不存在"<<endl;
	}
	return 0;
}

Please criticize and correct

おすすめ

転載: blog.csdn.net/qq_51231048/article/details/127166128