I get the gist of binary search, but I'm having trouble understanding why this incomplete (because I haven't specified side that I want the search to run on) code won't even return any value (such as for lists of small length).
class Solution:
def search(self, nums: List[int], target: int) -> int:
def bin_search(start, end, nums, target):
mid = int((start+end)/2)
if start >= end:
return -1
if nums[mid] == target:
return mid
elif nums[start] == target:
return start
elif nums[end] == target:
return end
bin_search(mid, end-1, nums, target)
bin_search(start+1, mid, nums, target)
output = bin_search(0, len(nums)-1, nums, target)
return output
Taken from the following leetcode: https://leetcode.com/problems/search-in-rotated-sorted-array/
I am aware of what this SHOULD look like - in those cases, people check only if the MID of the sub_list equals our target. I don't see why I shouldn't check the start and end either.
Additionally, the code above, even when it finds the index of the target, it won't properly return when recursion has ended.
Any advice is greatly appreciated. I do want to repeat, I realize I haven't specified which side the binary search should run on, but, I'm just trying to understand why it won't even return any value.
Thanks.