Binary Search Algorithm: Explanation and Python Tutorial

preview_player
Показать описание
Binary search is the powerhouse of computer science... Pretty much. One of the most basic introductory concepts in algorithms is binary search, but don't be fooled, binary search is incredibly powerful. Basically, the idea behind it is you are given a (sorted) list of items, you can leverage the sorted-ness to determine where something is much faster than scanning the whole list.

Let's start with guessing a random number between 1 to 10. One way we could do it is to scan the entire list one at a time.. 1, 2, 3... until we get to the right number. But, this is slow! Instead, we can use the computer's feedback to cut down the possibility space in half every time, if we start guessing from the middle. This is what we call divide-and-conquer. The whole gist of binary search is based on this principle.

In a sorted list, we can first guess the middle element and after receiving feedback, we can narrow down the possibilities to either side of that element (if it's not that element itself). We repeat this process until we have either found the element or determined that it's not present.

If our list is "n" elements long, binary search only takes a maximum of log(n) tries to find the answer, whereas linear (naive) search takes a maximum of n tries and average of n/2 tries. This means.. for a list of 1000 items, binary search takes 10 tries when linear search takes on average 500, but could take up to 1000 (and that's assuming the value you're looking for is in the list)!!

Understood? Good. Now we'll code it up and I'll show you exactly how much faster binary search is, compared to naive search!

Feel free to leave any questions.

Thanks for watching everyone!
~~~~~~~~~~~~~~~~~~~~~~~~
Рекомендации по теме
Комментарии
Автор

Thanks for watching!! Hope this video helped you guys learn something new :)

KylieYYing
Автор

I thoroughly enjoyed this video, thanks for taking the time to make it. I think what you are doing here is going to be a great, best of luck with it all, looking forward to what's next!

ulim
Автор

Easy & outstanding explanation. 😍 I tried a lot in YouTube to find an easy explanation of BST, but today my fighting with myself had ended up here. Thank You so much ☺️

AkashdeepDam
Автор

The way you edit your is Very unique. I love that. Thanks for the explanation. Also I have been following your 12 python project video and it's just amazing..
Again thanks for the efforts.

himanshumishra
Автор

you're incredible!! I have a computer science exam in 4 days and you just saved my life !!

digitella-
Автор

New series on Algorithms with Python Please...
Really good explanations and I love it.

htunnayaung
Автор

Learnt something about efficiency today. Many thanks!

ayodeleoluwatosin
Автор

Wonderful video, explain every single concept in it, great work

procode
Автор

1 minue in, and i now understand binary search! Thanks Kylie

Dan-wqid
Автор

Thanks ❤️.
You are the best instructor.

khanzubair
Автор

Another great video.... Love your contents

CodeWithTomi
Автор

Not sure why the heck you have <15k subscribers. Don't worry, people will smarten up soon. Keep up the great work Kylie!

ovey
Автор

Your awesome Kylie. Another great video Hun. Thanks 🥰

cryptombt
Автор

I really love your content, please make it a little more consistent. I'll really appreciate it and Also Thanks a lot for making such an amazing content.

salahiansofficial
Автор

So Helpful and Informative; Thanks A Shitload Sweetie, Love you and your Neural Circuits!!!

trtlphnx
Автор

Found this very helpful, can you please do more of this :)

david.theillusionist
Автор

I've started learning c, but haven't touched python a bit. Still, I could understand the logic behind your code. Programming is starting to get fun for me.

dfla
Автор

Thank you for your efforts and videos. I have question. In the way I learn it is easier to understand a concept if it can be applied to an everyday activity. When would a person use such a search? I am sorry if my questions is basic, but I hope to understand more. Thank you again.

srpskihayk
Автор

Can you make a whole series on automation ?

doggymama
Автор

Can't we get a kind of full course on algorithms?

khanzubair