From f3369d0c835403803a293745fd8e76e1b238f48b Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Sat, 13 May 2023 11:06:16 -0700 Subject: [PATCH 1/7] idk what I'm doing, I feel like I'm on the right track --- search.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/search.py b/search.py index 1c03df0..bb26d1c 100644 --- a/search.py +++ b/search.py @@ -19,3 +19,16 @@ Output: 0 Explanation: -1 exists in the list and its index is 0 """ + +def binary_search(arr): + min_idx = arr[0] + max_idx = len(arr) - 1 + mid_idx = int(max_idx / 2) + middle = arr[mid_idx] + + print(min_idx) + print(mid_idx) + print(middle) + print(max_idx) + +binary_search([-1,2,4,6,8]) \ No newline at end of file From f5a7574ba80cd65a8c64a7be7a4684a381d690eb Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Sat, 13 May 2023 11:31:47 -0700 Subject: [PATCH 2/7] adds pseudocode --- search.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/search.py b/search.py index bb26d1c..e7dfd8b 100644 --- a/search.py +++ b/search.py @@ -20,15 +20,22 @@ Explanation: -1 exists in the list and its index is 0 """ -def binary_search(arr): +def binary_search(arr, target): min_idx = arr[0] max_idx = len(arr) - 1 mid_idx = int(max_idx / 2) middle = arr[mid_idx] + + # keep track of minimum of the range + # keep track of the max of the range + # keep track of the middle of the range + # check if middle of the range equals target + # if not & mid is > target, check left half, if mid < target check right half + # loop this until value is found, or where index would be - print(min_idx) - print(mid_idx) - print(middle) - print(max_idx) + print("min idx: ", min_idx) + print("mid idx: ", mid_idx) + print("middle value: ", middle) + print("max idx: ", max_idx) -binary_search([-1,2,4,6,8]) \ No newline at end of file +binary_search([-1, 0, 3, 5, 9, 12], -1) \ No newline at end of file From dbbda26ca872e327891a4ab07b90e5b2018ae856 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Sat, 13 May 2023 12:20:56 -0700 Subject: [PATCH 3/7] completes iterative binary search function --- search.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/search.py b/search.py index e7dfd8b..9810de3 100644 --- a/search.py +++ b/search.py @@ -21,21 +21,28 @@ """ def binary_search(arr, target): - min_idx = arr[0] - max_idx = len(arr) - 1 - mid_idx = int(max_idx / 2) - middle = arr[mid_idx] - # keep track of minimum of the range + min_idx = 0 # keep track of the max of the range - # keep track of the middle of the range - # check if middle of the range equals target - # if not & mid is > target, check left half, if mid < target check right half - # loop this until value is found, or where index would be - - print("min idx: ", min_idx) - print("mid idx: ", mid_idx) - print("middle value: ", middle) - print("max idx: ", max_idx) + 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 -binary_search([-1, 0, 3, 5, 9, 12], -1) \ No newline at end of file +print(binary_search([ 1, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 ], 2)) \ No newline at end of file From 438b08661204701e5fcbca7b649f3bca18394b33 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Sat, 13 May 2023 12:28:54 -0700 Subject: [PATCH 4/7] starts recursive search function --- recursive-search.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 recursive-search.py diff --git a/recursive-search.py b/recursive-search.py new file mode 100644 index 0000000..5704021 --- /dev/null +++ b/recursive-search.py @@ -0,0 +1,25 @@ +'''' +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): + # keep track of minimum of the range + min_idx = 0 + # keep track of the max of the range + max_idx = len(arr) - 1 + + + + # if not found in arr + return -1 + +recursive_binary_search([-1, 0, 3, 5, 9, 12], -1) \ No newline at end of file From da1687f49bc5fe25054a3eec072bee75d4a4de8f Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Sat, 13 May 2023 18:44:02 -0700 Subject: [PATCH 5/7] fixes empty arrays --- recursive-search.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/recursive-search.py b/recursive-search.py index 5704021..52c20dd 100644 --- a/recursive-search.py +++ b/recursive-search.py @@ -5,21 +5,34 @@ 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): - # keep track of minimum of the range - min_idx = 0 - # keep track of the max of the range - max_idx = len(arr) - 1 - + 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 + - # if not found in arr - return -1 -recursive_binary_search([-1, 0, 3, 5, 9, 12], -1) \ No newline at end of file +print(recursive_binary_search([-1, 0, 3, 5, 9, 12], 12)) \ No newline at end of file From 776b6eba5f5ba1f9c1f303fae92512a4a40a23ba Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Sat, 13 May 2023 18:47:04 -0700 Subject: [PATCH 6/7] finishes recursive algorithm --- recursive-search.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/recursive-search.py b/recursive-search.py index 52c20dd..5b676c2 100644 --- a/recursive-search.py +++ b/recursive-search.py @@ -27,12 +27,10 @@ def recursive_binary_search(arr, target): return recursive_binary_search(arr[:mid_idx], target) else: # check mid_idx + 1 - last idx - result = recursive_binary_search(arr[mid_idx + 1], target) + 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], 12)) \ No newline at end of file +print(recursive_binary_search([-1, 0, 3, 5, 9, 12], 5)) \ No newline at end of file From 9587906845c06102b394d44ef9632abbef9c998c Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Mon, 15 May 2023 14:32:14 -0700 Subject: [PATCH 7/7] finishes review in class --- search.py | 100 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 24 deletions(-) diff --git a/search.py b/search.py index 9810de3..4d4e346 100644 --- a/search.py +++ b/search.py @@ -20,29 +20,81 @@ 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 +# 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: - max_idx = mid_idx - 1 - # print(arr[mid_idx]) - # returns -1 if target not found in array - return -1 + 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, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 ], 2)) \ No newline at end of file +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