LeetCode Search in Rotated Sorted Array Binary Search (golang)

Problem
在这里插入图片描述
Basics
Binary Search
Binary Search is a more efficient method of finding data. However, Binary Search requires a linear table to be stored sequentially, and the elements in the table are arranged in order by keyword

Search Process
First, assuming that the elements in the table are arranged in ascending order, the keywords recorded in the middle of the table are compared with the search keywords. If they are equal, the search succeeds. Otherwise use middle position after the record before the table is divided into two child table, if the middle record keyword is greater than the search keywords, further to find the former child table, or further search after a child table, repeat the process until you find meet the conditions of records, to find success, or until the child table does not exist, the search is not successful

Time complexity
The basic idea of binary search is to divide n elements into roughly equal parts,compare a[n/2] with x,if x=a[n/2],then finds x,process ends.If x<a[n/2],just continue searching for x in the left half of array a.If x>a[n/2],continue searching for x in theright half of array a
The time complexity is the number of cycles
There’s n of them, and it’s going to follow n,n/2,n/4,…n/2^k where k is the number of cycles
beacuse n/2^k rouned equal 1
set n/2^k=1
obtains k=log2n

Go Source Code

func BinarySearch(n []int, target int) int {
   length := len(n)   
   low := 0   
   high := length - 1
   for low <= high {
         mid := (low + high) / 2      
         if n[mid] > target {
             high = mid - 1
         } else if n[mid] < target {
             low = mid + 1      
         } else {         
             return mid      
         }   
    }
    return -1
}

Analysis Process
In this problem,an array sorted in ascending order is rotated at some pivot unknown to you beforehand.So we need to divide the array into two parts ,then use Binary Search.

Part One The first half is ascending

nums[left] <= nums[mid]
1.if nums[left] <= target <= nums[mid] ,tagert is in the first half of the ordered range right = mid - 1
2.if target < nums[left] <= nums[mid],target is less than the minimum value in the first half of the ascending sequence, can not be found in the first half of the sequence, need to find in the second half of the sequence left = mid + 1
3.If nums[left] <= nums[mid] < target,target is greater than the maximum value in the first half of the ascending sequence, and cannot be searched in the first half of the sequence, need to be searched in the second half of the sequence left = mid + 1

The same goes for the second half

Code

func search(nums []int, target int) int {
	left := 0
	right := len(nums) - 1 
	for left <= right {
		mid := (left + right) / 2        //find the median
		if nums[mid] == target {
			return mid         //determine whether to look in the first half
		}
		if (nums[left] <= target && target <= nums[mid]) || (nums[mid] <= nums[right] && (target < nums[mid] || target > nums[right]))  {       //determine which part the target is
			right = mid - 1
		} else {
			left = mid + 1
		}
	}
    return -1
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值