C++数据结构与算法\折半查找(二分查找)BinarySearch()

5、折半查找(二分查找)

简介:

折半查找只能用于已经排过序的数据。 没排过序的就只能用顺序查找法。

折半查找法效率比较高, 100万个数据可能只需要查找20次, 而顺序查找可能就要找50,0000次

int BinarySearch(int* List, const int N, const int X)
{
	// 先假设有一个数组, 是从1-100W排过序的数组。
	int Lower = 0, Upper = N - 1, Mid;		// 最小数/最大数/中间数
	// 如果 最小数 还小于等于 最大数,那表明折半还没结束,继续折半循环查找。
	while (Lower <= Upper)
	{
		// 中间数 = 最小数 + 最大数(在上边已经-1) / 2。
		Mid = (Lower + Upper) / 2;
		// 如果这个中间数刚好就是要找的数,就返回它的下标。
		if (List[Mid] == X) return Mid;
		// 如果 中间数 不是我们要找的,那就比较一下要找的数比 中间数 小还是大。
		else if (List[Mid] < X)
		{
			// 进来这条语句了,那说明要找的数肯定比 中间数 大
			/* 把 最小数 等于 中间数 + 1,根据刚开始的假设,Mid现在 = 50W
			也就是把 最小数 设置50W01,然后继续折半查找。 */
			Lower = Mid + 1;
		}
		// 如果 要找到的数 小于 中间数,那比 中间数 大的数就不用找了
		else if (List[Mid] > X)
		{
			// 也就是说 从 499999 个数开始找
			Upper = Mid - 1;
		}
	}
	// 如果执行到这里,表明循环都结束了,还没找到要找的数并返回这个数的下标,那就表示执行失败。
	return -1;
}
int main()
{
	int NumA[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
	int NumA_Size = sizeof(NumA) / sizeof(int);
	int numa;

	cout << "请输入一个要查找的数字(1-20) >";
	cin >> numa;
	// 这个函数返回找到的数的下标,而不是数字本身。
	int ResultA = BinarySearch(NumA, NumA_Size, numa);
	if (ResultA == -1)
	{
		cout << "数字未找到!" << endl;
	}
	else
	{
		cout << "数字 >" << numa << "被找到!下标是 >" << ResultA << endl;
	}
	return 0;
}

备注:使用二分查找必须保证数据已经被排序!没被排序的可以用慢方法:顺序查找。
顺序查找法:https://blog.csdn.net/WenRou21_/article/details/105756140

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值