Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
iterative binary search completed
  • Loading branch information
Hunter315 committed Jan 17, 2019
commit 9f58fc5407aac066f03b1a777be2caad3eda9522
11 changes: 11 additions & 0 deletions project/searching.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ def linear_search(arr, target):
# STRETCH: write an iterative implementation of Binary Search
def binary_search(arr, target):


if len(arr) == 0:
return -1 # array empty

low = 0
high = len(arr)-1

# TO-DO: add missing code
low = 0
high = len(arr) - 1
while low <= high:
mid - low + (high - low) // 2
mid_val = arr[mid]
if mid_val == target:
return mid
elif mid_val < value:
low = mid + 1
else: high = mid - 1

return -1 # not found

Expand Down