DEV Community

Cover image for Python binary search
Budy mnvenom
Budy mnvenom

Posted on • Originally published at blog.icodes.tech

Python binary search

Source Python Binary search explained with examples

Binary search algorithm

a binary search does something very similar. So it relies on having a sorted list so you have this list. Let's say it's 1024 items long. And what you do with binary search is you go to the middle item of this sorted list and if that's the item you're looking for then that's it.The search is over.

if the item is not the item you're looking for but is higher than it in numerical value or in the latter value depending what it is that you're searching for. Then you can say well the item that I'm looking for must be in the left part of the list because I know these are in the correct order they're sorted.

ezgif-7-e1fca7ee4861.gif

So if I've landed on the midpoint of this list and I'm the item I land on is higher in value than the item I'm looking for.I can get rid of all of the items to the right of me to the to the right of the item that I've landed on because I know that they are also going to be too high. And then you repeat that process.
Source Python Binary search explained with examples

Discussion (0)