8000 Update binary_search.py by SOUMEE2000 · Pull Request #129 · AllAlgorithms/python · GitHub
[go: up one dir, main page]

Skip to content

Update binary_search.py #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
0