Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
Binary search will closet to smaller element (also the larger element will not be the target), so let b = mid + 1 to prevent dead loops.
public int findMin(int[] nums) {
int b = 0, e = nums.length - 1;
while(b < e){
int mid = (e - b) / 2 + b;
if(nums[mid] >= nums[b] && nums[mid] >= nums[e]){
b = mid + 1;
}else e = mid;
}
return nums[b];
}