From c692648dad8f888b3c05d2768f1bc7f3311119fd Mon Sep 17 00:00:00 2001 From: Thomas Sabino-Benowitz Date: Wed, 16 Sep 2020 18:51:19 -0400 Subject: [PATCH] circling back to finish merge function again --- src/searching/searching.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/searching/searching.py b/src/searching/searching.py index fbe0717f..e1fcaac2 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,7 +1,33 @@ # TO-DO: Implement a recursive implementation of binary search def binary_search(arr, target, start, end): + # Your code here +# we need to call this function in itself +# each recursive call should bring us closer to reaching +# the base case(s) +# base case(s): when does the recursion stop? +# it stops when we find the target +# or start and go past each other (i.e. we didn't find the target) + + if start > end: + # if target isn't in the array + return -1 + else: + mid = (start + end) // 2 + if arr[mid] == target: + # this is our second base case + return mid + # otherwise we need to move towards one of our base cases + # arr doesn't change, just because we'll end updating start and end target + # doesn't change between recursive calls + # and changes if we're tossing out the right side of the array + elif arr[mid] > target: + binary_search(arr, target, start, mid - 1) + + else: + return binary_search(arr, target, mid + 1, end) + # STRETCH: implement an order-agnostic binary search # This version of binary search should correctly find @@ -10,5 +36,4 @@ def binary_search(arr, target, start, end): # You can implement this function either recursively # or iteratively def agnostic_binary_search(arr, target): - # Your code here - + # Your code here \ No newline at end of file