从排序的旋转数组中找原开始元素

给定一个排序数组,经过旋转,找出原起始元素。

Given a sorted list but it is rotated. Find the start point in that list

由于数组使排好序的,但是从第k个元素开始记过了旋转。所以可以使用二分查找来找到起始元素,假设为最小元素。

Hey folks. I think you are all missing a very important hint that the array is sorted and rotated by some K positions. In that case we should use the binary search principle to find the reset point, which will be the minimum element in the array. Below is the complete code to demonstarte this technique that runs in O(Log N) time:

int find_reset_point(int arr[],int start ,int end)
{
	if (start>end)
	{
		return -1;
	}
	int midd;
	while (start<end)
	{
		
		if (end - start <= 1)
		{
			if (arr[start]>arr[end])
			{
				return arr[end];
			}
		}
		midd = (start+end)>>1;
		if (arr[midd]>arr[start])//从中间元素开始考查,如果中间元素大于start元素,表面要搜索的点在midd到end之间
		{
			start = midd;
		}
		else//否则要搜索的点start到midd之间
			end = midd;
	}
	return -1;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值