Question Locked

Python Binary Search Algorithm Error

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello this is Gulshan Negi
Well, I am creating a program for binary search algorithm using Python program but there is a problem in its execution, I don't know what I am doing wrong here.
Source Code:-

def binary_search(a_list, an_item):
first = 0
last = len(a_list) - 1

while first <= last:
mid_point = (first + last) // 2
if a_list[mid_point] == an_item:
return True
else:
if an_item < a_list[mid_point]:
last = mid_point - 1
else:
first = mid_point + 1
return False

def binary_search_rec(a_list, first, last, an_item):
if len(a_list) == 0:
return False
else:
mid_point = (first + last) // 2
if a_list[mid_point] == an_item:
return True
else:
if an_item < a_list[mid_point]:
last = mid_point - 1
return binary_search_rec(a_list, first, last, an_item)
else:
first = mid_point + 1
return binary_search_rec(a_list, first, last, an_item)

if __name__ == '__main__':
a_list = [1, 4, 7, 10, 14, 19, 102, 2575, 10000]

print('Binary Search:', binary_search(a_list, 4))
print('Binary Search Recursive:',
binary_search_rec(a_list, 0, len(a_list) 1, 4))

Can anyone give their suggestions on this, would be a great support in this regard.
Thanks

Note: promotional URL removed by moderator.

3 replies

by kees_b edited

You don't tell what problem you get, and that makes it difficult to offer a solution.

if you learn programming, as you seem to do, you must also learn to debug. Either runtime, by setting breakpoints and looking at the variables, or by printing intermediate results after each step.

I'm sure you'll manage to solve this.

by rproffitt edited

"There are a few issues with the code:

The subtraction symbol in lines 2 and 20 appears to be an en-dash instead of a hyphen, which can cause a syntax error. Replace these with regular hyphens.

In the recursive binary search function, the base case should check if first > last instead of checking if the list is empty. Change the if statement in line 12 to if first > last:

Here's the corrected code:
def binary_search(a_list, an_item):
first = 0
last = len(a_list) - 1

while first <= last:
mid_point = (first + last) // 2
if a_list[mid_point] == an_item:
return True
else:
if an_item < a_list[mid_point]:
last = mid_point - 1
else:
first = mid_point + 1
return False

def binary_search_rec(a_list, first, last, an_item):
if first > last:
return False
else:
mid_point = (first + last) // 2
if a_list[mid_point] == an_item:
return True
else:
if an_item < a_list[mid_point]:
last = mid_point - 1
else:
first = mid_point + 1
return binary_search_rec(a_list, first, last, an_item)

if __name__ == '__main__':
a_list = [1, 4, 7, 10, 14, 19, 102, 2575, 10000]

print('Binary Search:', binary_search(a_list, 4))
print('Binary Search Recursive:', binary_search_rec(a_list, 0, len(a_list) - 1, 4))

Note that in the main function, the second call to binary_search_rec has a typo where len(a_list) 1 should be len(a_list) - 1. This has been fixed in the corrected code above."

by gulshan212 edited

First of all thanks a lot for your kind response.
Well, it works thanks.

This discussion is closed to new replies.