二分查找模板 - BinarySearch

引言:
据说90%的程序员不能正确的实现二分查找,你可以先自己试试哈,反正我写了好久才改对。
模板题目:
Binary Search
题目描述
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<bits/stdc++.h>
using namespace std;
const int maxn=5000005;
int a[maxn],n,k;
int binarySearch(int a[],int n,int k){
	int l=1,r=n,mid=(l+r)/2;
	while(l<=r){
		mid=(l+r)/2;
		if(a[mid]==k) return mid;
		else if(a[mid]>k) r=mid-1;
		else if(a[mid]<k) l=mid+1;
	}
	return -1;
}
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	int num=binarySearch(a,n,k);
	cout<<num<<endl;
	return 0;
}

说明:
每个人有自己的写法,这是我自己最习惯的写法,反正写对就好,实在写不对,就调库吧…

发布了65 篇原创文章 · 获赞 96 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tran_sient/article/details/105004871
今日推荐