DEV Community

Cover image for Python binary search
Budy mnvenom
Budy mnvenom

Posted on • Originally published at blog.icodes.tech

2

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay