Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Open
Changes from all commits
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
Update binary_search.py
Reduced redundancy in code by updating the middle element at the beginning of the loop
  • Loading branch information
SOUMEE2000 authored Feb 27, 2021
commit cd54e55f09ad58281660a5d2cdfb035d5e5ec71c
12 changes: 5 additions & 7 deletions algorithms/sorting/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def binary_search(value, list_to_search):
search_value = -1
max_index = len(list_to_search) - 1
min_index = 0
middle_index = int(math.floor( (max_index + min_index) / 2))
current_element = list_to_search[middle_index]


while max_index >= min_index:

middle_index = int(math.floor( (min_index + max_index) / 2))
current_element = list_to_search[middle_index]

if current_element == value:
return current_element
Expand All @@ -38,10 +39,7 @@ def binary_search(value, list_to_search):

elif value < current_element:
max_index = middle_index - 1

middle_index = int(math.floor( (min_index + max_index) / 2))
current_element = list_to_search[middle_index]


return search_value


Expand Down