欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

有序表查找--二分法查找算法 c++实现

程序员文章站 2024-03-17 14:43:16
...
二分法有序表查找:
int binary_search (vector<int>vec ,int key,int low, int high)
{
	int mid;
	while (low <= high)
	{
		mid = (low + high )/ 2;
		if (key < vec[mid])
			high = mid - 1;
		else if (key > vec[mid])
			low  = mid + 1;
		else // key > vec[mid]
			return mid	;
	}
	cout << "no find object value   " << key << ",  between  " << low << "  to  " << high << endl;
	return 0;
}
相关标签: 数据库