Binary Search Algorithm Explained (Full Code Included) - Python Algorithms Series for Beginners

preview_player
Показать описание
This video is a part of a full algorithm series. Check them out here:

#Python #Algorithm #BinarySearch

Binary Search Takes a sorted sequence of elements and figures out if a given element is within that sequence. We'll do this with a series of repeated comparisons. We compare the middle number of our sequence to the item we're searching for. This determines if we continue looking right or left of the midpoint.

The Binary Search Algorithm has a complexity of log2(n) because it doesn't matter how many elements we pass to the algorithm. We'll still cut the entire data set in half each iteration, so the results will happen pretty quickly.

Thanks so much for all the continued support! 5780 subscribers at the time of writing. That's incredible. Thank you all for your continued support of the channel.

Join The Socials -- Picking Shoutouts Across YouTube, Insta, FB, and Twitter!
*****************************************************************
Full code from the video:

# "Angled brackets not allowed in youtube description."
def binary_search(sequence, item):
begin_index = 0
end_index = len(sequence) - 1

while begin_index #less than= end_index:
midpoint = begin_index + (end_index - begin_index) // 2
midpoint_value = sequence[midpoint]
if midpoint_value == item:
return midpoint

elif item #less than midpoint_value:
end_index = midpoint - 1

else:
begin_index = midpoint + 1

return None

sequence_a = [2,4,5,6,7,8,9,10,12,13,14]
item_a = 3

print(binary_search(sequence_a, item_a))

Packages (& Versions) used in this video:
Python 3.7

*****************************************************************
Code from this tutorial and all my others can be found on my GitHub:

Check out my website:

If you liked the video - please hit the like button. It means more than you know. Thanks for watching and thank you for all your support!!

--- Channel FAQ --

What text editor do you use?

What Equipment do you use to film videos?

What computer do you use/desk setup?

What editing software do you use?
Premiere Pro for video editing
Photoshop for images
After Effects for animations

Do I have any courses available?
Yes & always working on more!

Where do I get my music?
I get all my music from the copyright free Youtube audio library

Let me know if there's anything else you want answered!

-------------------------

Always looking for suggestions on what video to make next -- leave me a comment with your project! Happy Coding!
Рекомендации по теме
Комментарии
Автор

Man I really wish you would bring this series back for other algo techniques, you are my fav python instructor on YT

kiraisonline
Автор

Thanks a lot for this videos man, they are so simple, but they explain everything in perfect detail

osvaldogarcia
Автор

You explained this like I'm 5 years old and thank you bc it makes perfect sense now 😭🙌🏾

MyLoweLife
Автор

Thank you for your videos. I came across your algorithm series this morning and spent the day watching all 5 videos and learning from your examples. Now I can use them to make my own Python library of tips & tricks to use when I program. Keep up the good work!

jimrakel
Автор

You are amazing! I just started my Data Structures and Algorithms course, and I almost had a heart attack as we covered both linear and binary search algorithms and now have to do math problem for the quiz. Both (0)logn and theta problems. But the most important thing is to understand the logic and what the code is doing. You're doing an awesome job at explaining this!! Thank you.

sechvnnull
Автор

Thanks alot man for the tutorial very well explained you deserve more subs

salvationprayerfellowship
Автор

Thanks for the tutorials Derrick they are so easy to understand and well explained. I’m self learning Java and Python for the past three months and your videos are very helpful. Good Teacher.

in-thegarden
Автор

Thank you very much for this amazing video. I've been searching everywhere for this but there are too complex. But this one is...Schway!!😀

solomontaiwoabbaly
Автор

Excellent tutorial. Love the algorithm explanation at the end to re-iterate our understanding.

ahyungrocks
Автор

I'm cramming for my first face-to-face technical interview in 2 weeks and I have to say that I wish I found your channel sooner. Thank you for your effort in making these videos.

hb
Автор

You are young and smart. I feel so inadequate in regard to thinking capacity 😭. But your videos are amazing, please don’t stop!

msms
Автор

You are doing a great job man. The videos are short, well explained and very easy to understand. Please keep posting.

murtazahonarpoor
Автор

Really loved this. Simple and precise. Thanks

elijahlair
Автор

and also for midpint can be (start + end )//2

redachikhaoui
Автор

Hey Derrick, this is the first time I came across your video and it is so articulate and lucid . I am already subscribing and looking forward to learn more from you .

GG-lebq
Автор

When I studying this using the below list :
sequence_a = [1, 2, 4, 5, 6, 7, 8, 9, 12, 14, 16, 17, 22]
list_a = 1

it will return 0 as sequence, which looks a bit strange to me...so I add +1 to it, haha

print(binary_search(sequence_a, list_a)+1)

Btw, it's a good video! Thanks for teaching me!

toby
Автор

For anyone wondering.
This is a recursive solution.

a = [1, 2, 3, 4, 5, 6]

def binarySearch(value, low, high, list):
if low <= high:
middle = (low+high) // 2

if list[middle] == value:
return f"Value {value} is located at Index {middle}"

#here we need to check the lower half as the value is smaller than midpoint
if list[middle] > value:
return binarySearch(value, low, middle-1, list)

#here we need to check the upper half as the value is bigger than midpoint
elif list[middle] < value:
return binarySearch(value, middle+1, high, list)

else:
return f"Value {value} is not in the list!"




print(binarySearch(6, 0, len(a), a))

nonserviam
Автор

Cannot tell if he is smiling or being held at gun point with those facial expressions lol. At times he seems forced other times he generally seems interested.

defaltcm
Автор

Binary Search Implementation (I tried before seeing your approach):


def binary_search(seq, val):
print(f"Base Array: {seq}")
seq.sort()
print(f"Sorted array: {seq}")
midpoint = len(seq)//2
while seq[midpoint] != val:
if val > seq[midpoint]:
midpoint = midpoint + len(seq[midpoint+1:])//2
elif val < seq[midpoint]:
midpoint = len(seq[:midpoint])
return f"value is at {midpoint} index in {seq}"

teen_python_go
Автор

Thanks a lot for the series!! I watch it all

marioandresheviacavieres