From a925a30ec4068adfb9f25a28293497f920996e11 Mon Sep 17 00:00:00 2001 From: Shivchopra82 <86181878+Shivchopra82@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:44:30 +0530 Subject: [PATCH] Update binarysearch.py Array index starts with 0 but len(numver_list) is giving last index as 8 instead of seven by correcting this we don't need that extra if condition above. --- algorithms/1_BinarySearch/binarysearch.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/algorithms/1_BinarySearch/binarysearch.py b/algorithms/1_BinarySearch/binarysearch.py index ed3413e..6b4e9d6 100644 --- a/algorithms/1_BinarySearch/binarysearch.py +++ b/algorithms/1_BinarySearch/binarysearch.py @@ -32,9 +32,6 @@ def binary_search_recursive(numbers_list, number_to_find, left_index, right_inde return -1 mid_index = (left_index + right_index) // 2 - if mid_index >= len(numbers_list) or mid_index < 0: - return -1 - mid_number = numbers_list[mid_index] if mid_number == number_to_find: @@ -51,5 +48,5 @@ def binary_search_recursive(numbers_list, number_to_find, left_index, right_inde numbers_list = [12, 15, 17, 19, 21, 24, 45, 67] number_to_find = 21 - index = binary_search_recursive(numbers_list, number_to_find, 0, len(numbers_list)) - print(f"Number found at index {index} using binary search") \ No newline at end of file + index = binary_search_recursive(numbers_list, number_to_find, 0, len(numbers_list)-1) + print(f"Number found at index {index} using binary search")
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: