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
}

猜你喜欢

转载自blog.csdn.net/qq_46595591/article/details/107506059
今日推荐