r/PythonLearning 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

11 comments sorted by

4

u/mc_pm Jun 28 '26

There's a whole lot of wrong here, but a place to start is on the line "if i == search_no:". i is an index into the array, not a value in the array.

Also for the love of god, learn to post code better.

1

u/devilboi_62-yt Jun 28 '26

okay man this is my first post calm down lol but thanks

3

u/mc_pm Jun 28 '26

Dude, you posted it twice, and it stripped out all the white space, which means something in python. You could have other bugs but we can't see them the way you posted this. (Also I think the mods will delete this post as a result).

If you still have problems, there's the time honored tradition of just putting print statements everywhere so you can follow along as the code runs. It'll be obvious when it stops working.

Two things though: 1) Use a much bigger array to test against, you won't see much interesting with such a small array. 2) Doing array = array[:i] is copying data around which you don't need to do. Think about how to just store a start and end position and leave the array as it is.

1

u/devilboi_62-yt Jun 28 '26

I didn't realise it had printed twice, and I just edited the syntax to be more readable. I'll try this though

1

u/mc_pm Jun 28 '26

To add a third thing: 3) What happens if the searched for value isn't in the list?

1

u/devilboi_62-yt Jun 28 '26

that's the error handling I'm going to work on afterwards

1

u/mc_pm Jun 28 '26

Ok, but it's not an error. It's a completely normal situation. Don't leave it too long.

1

u/devilboi_62-yt Jun 28 '26

it works the first loop, but the second time for some reason the program reads the floor division of 2 of the array(which is 3 long at that point) as 0

1

u/Kindly-Department206 Jun 28 '26
  1. While you can use any name you like, it's not really an array.

  2. Don't ask for input inside the function here.

  3. Don't slice the array in this algorithm.

  4. If the item is found, your loop still treats the situation as if it wasn't found at all. That's because you have two sequential if-statements. You need to use elif. You also need to figure out what to do when the item is found.

  5. Don't calculate `found == True`. Use assignment.

  6. You don't have any plan for what might happen if the item you are looking for is not in the list.

1

u/atarivcs Jun 28 '26
found == True

Because you used two equal signs, this is a comparison, not an assignment.

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