C++二分查找法改进版

这里针对数组中有多个重复元素对二分查找法进行了如下的改进
floor函数:在arr中寻找target,如果存在,则返回target的最小索引,如果不存在,则返回比target小的那个值的最大索引 ,如果target比arr中最小的元素都还要小,则说明target不在数组arr中,则返回-1

ceil函数:/在arr中寻找target,如果存在,则返回target的最大索引,如果不存在,则返回比target大的那个值的最小索引,如果target比arr中比最大的元素都还要大,则说明target不在数组arr中,则返回-1

顺便推荐一个课程,我最近的算法学习都是从这个课程上学来的,包括这个的代码啊,也是仿照着bobo老师的代码后写出来的
https://coding.imooc.com/class/71.html

#include<iostream>
#include<cassert>
#include<ctime>

using namespace std;

//在arr中寻找target,如果存在,则返回target的最小索引,如果不存在,则返回比target小的那个值的最大索引
//如果target比arr中最小的元素都还要小,则说明target不在数组arr中,则返回-1
template<typename T>
int floor(T arr[], int n, T target)
{
	assert( n >= 0);  //这一个忘记了
	int l,r;
	l = -1; r = n-1;  //如果这里是l = 0的话,就会找到的第一个值是1

	if(target < arr[l+1])
		return -1;

	while(l < r)
	{
		int mid = l + (r-l+1)/2;
		if(arr[mid] >= target)
			r = mid-1;
		else
			l = mid;
	}

	assert(l == r);

	if( l + 1 < n && arr[l+1] == target)
		return l + 1;

	return l;


}

//在arr中寻找target,如果存在,则返回target的最大索引,如果不存在,则返回比target大的那个值的最小索引
//如果target比arr中比最大的元素都还要大,则说明target不在数组arr中,则返回-1
template<typename T>
int ceil(T arr[],int n, T target)
{
	assert(n >= 0);

	int l,r;
	l = 0; r = n;

	if(arr[r-1] < target)
		return -1;

// 寻找比target大的最小索引值
	while(l < r)
	{
		int mid = l + (r-l)/2;  //这里是 l-r
		if(arr[mid] > target)   //这里不能 >=
			r = mid;
		else
			l = mid + 1;

	}

	assert(l == r);

	if(r - 1 >= 0 && arr[r-1] == target)
		return r-1;
	return r;

}



int main(int argc, char const *argv[])
{
	//int *arr = new int[10];
	int a[] =  {1, 1, 1, 2, 2, 2, 2, 2, 4, 4, 5, 5, 5, 6, 6, 6};
	int n = sizeof(a) / sizeof(int);
	for(int i = 0; i <= 8; i++)
	{
		int floorIndex = floor(a,n,i);
		cout << "the floor index of " << i << " is " << floorIndex << ".";
		cout << endl;

		if(floorIndex >= 0 && floorIndex < n)
			cout << "The value is " << a[floorIndex] << ".";
		cout << endl;

		int ceilIndex = ceil(a,sizeof(a)/sizeof(int),i);
		cout << "the ceil index of " << i << " is " << ceilIndex << ".";
		cout << endl;

		if(ceilIndex >= 0 && ceilIndex < n)
			cout << "The value is " << a[ceilIndex] << ".";
		cout << endl;

		cout << endl;
	}
	

	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五月的天气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值