diff --git a/recursive-search.py b/recursive-search.py new file mode 100644 index 0000000..5b676c2 --- /dev/null +++ b/recursive-search.py @@ -0,0 +1,36 @@ +'''' +MATHEMATICS•LINGUISTICS +relating to or involving the repeated application of a rule, definition, or procedure to successive results. +"this restriction ensures that the grammar is recursive" +COMPUTING +relating to or involving a program or routine of which a part requires the application of the whole, so that its explicit interpretation requires in general many successive executions. +"a recursive subroutine" +''' +# Example 4: +# Input: nums = [-1, 0, 3, 5, 9, 12], target = -1 +# Output: 0 +# Explanation: -1 exists in the list and its index is 0 + +def recursive_binary_search(arr, target): + if len(arr) == 0: + return -1 + + # find the middle index + mid_idx = int(len(arr) / 2) + # check if mid index equals target + middle = arr[mid_idx] + + if middle == target: + return mid_idx + elif middle > target: + # check idx 0 - mid_idx + return recursive_binary_search(arr[:mid_idx], target) + else: + # check mid_idx + 1 - last idx + result = recursive_binary_search(arr[mid_idx + 1:], target) + if result == -1: + return -1 + else: + return result + mid_idx + 1 + +print(recursive_binary_search([-1, 0, 3, 5, 9, 12], 5)) \ No newline at end of file diff --git a/search.py b/search.py index 1c03df0..4d4e346 100644 --- a/search.py +++ b/search.py @@ -19,3 +19,82 @@ Output: 0 Explanation: -1 exists in the list and its index is 0 """ + +# def binary_search(arr, target): + # # keep track of minimum of the range + # min_idx = 0 + # # keep track of the max of the range + # max_idx = len(arr) - 1 + + # # loop this until value is found, or where index would be + # while min_idx < max_idx: + # # keep track of the middle of the range + # mid_idx = int((min_idx + max_idx) / 2) + # middle = arr[mid_idx] + # # check if middle of the range equals target + # if middle == target: + # return mid_idx + # # if mid < target, narrow in on right side + # elif middle < target: + # min_idx = mid_idx + 1 + # # print(arr[mid_idx]) + # # if mid > target, narrow in on left half + # else: + # max_idx = mid_idx - 1 + # # print(arr[mid_idx]) + # # returns -1 if target not found in array + # return -1 + +def binary_search(nums, target): + # keep track of the window -- start by assuming the entire list is the window + # vars for low idx of window, high idx of window, mid idx of window + low = 0 + high = len(nums) + mid = high // 2 + + if target == nums[0]: + return 0 + + # continue to iterate until the target is found or is not here + while True: + # check the value at mid idx against target, stop if equal + if nums[mid] == target: + return mid + # if the value at the mid idx is less than target, slide window left + elif nums[mid] > target: + high = mid + difference = high - low + mid = low + (difference // 2) + # otherwise slid to right + else: + low = mid + difference = high - low + mid = low + (difference // 2) + print(f"low: {low}, mid: {mid}, high: {high}") + # stop iteration when the mid value is equal to low or high becasue we did not find it + if mid <= low or mid >= high: + return -1 + +print(binary_search([-1, 0, 3, 5, 9, 12], 2)) + +""" +Example 1: +Input: nums = [-1, 0, 3, 5, 9, 12], target = 9 +Output: 4 +Explanation: 9 exists in nums and its index is 4 + +Example 2: +Input: nums = [-1, 0, 3, 5, 9, 12], target = 2 +Output: -1 +Explanation: 2 does not exist in nums so return -1 + +Example 3: +Input: nums = [-1, 0, 3, 5, 9, 12], target = 12 +Output: 5 +Explanation: 12 exists in the list and its index is 5 + +Example 4: +Input: nums = [-1, 0, 3, 5, 9, 12], target = -1 +Output: 0 +Explanation: -1 exists in the list and its index is 0 +""" \ No newline at end of file