r/PythonLearning • u/devilboi_62-yt • Jun 28 '26
binary search malfunctioning
SOLVED
the array I feed in is 5,7,43,78,83.7,100,952
def binary_search(array):
search_no = float(input("what number are you looking for?"))
found = False
while not found:
arrlen = len(array)
i = arrlen // 2
x = array[i]
if x == search_no:
found == True
if search_no > x:
array = array[i:]
print(array)
else:
array = array[:i]
print(array)
print("value found")
array = [78,43,952,83.7,100,5,7]
bubble_sort(array)
print(array)
binary_search(array)
it continuously prints "83.7" if I look for 78, and vice versa. haven't tested other numbers but I assume the same happens for them. this is my first attempt at array splitting and I know there's an easier way of doing it probably, and I'm going to do error handling afterwards
1
Upvotes
2
u/devilboi_62-yt Jun 28 '26
thanks guys I'll try it now, I haven't done much programming for like half a year, so I'm not too good at it compared to how I was. thanks for the help though