From cd54e55f09ad58281660a5d2cdfb035d5e5ec71c Mon Sep 17 00:00:00 2001 From: Soumee Mukherjee <52605586+SOUMEE2000@users.noreply.github.com> Date: Sat, 27 Feb 2021 09:46:51 +0530 Subject: [PATCH] Update binary_search.py Reduced redundancy in code by updating the middle element at the beginning of the loop --- algorithms/sorting/binary_search.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/algorithms/sorting/binary_search.py b/algorithms/sorting/binary_search.py index afcfbaa..cef4e2b 100644 --- a/algorithms/sorting/binary_search.py +++ b/algorithms/sorting/binary_search.py @@ -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 @@ -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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy