Skip to content
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 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.
  • Loading branch information
Shivchopra82 authored Oct 21, 2021
commit a925a30ec4068adfb9f25a28293497f920996e11
7 changes: 2 additions & 5 deletions algorithms/1_BinarySearch/binarysearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
index = binary_search_recursive(numbers_list, number_to_find, 0, len(numbers_list)-1)
print(f"Number found at index {index} using binary search")