Swift Fun Algorithms #2: Binary Search

preview_player
Показать описание
Searching is very important when it comes to programming. Doing it efficiently is when it comes dealing with large sets of data is quite important as well. For example, imagine a company like Facebook having huge sets of data. If searching for a contact in your friends list was slow, user experience would be affected dramatically.

Full implementation of the code can be found here:

Рекомендации по теме
Комментарии
Автор

Someone should give you a noble prize for Great Tutorial

mushyunboxing
Автор

One of the best swift videos out there on YouTube

cinquain
Автор

First off, thanks so much for making these videos! I have learned so much from your channel.

I have read a couple comments on this video about omitting minus one and the plus one.

After doing some testing, my algorithm was actually unable to find the searchValue when it was equal to 2. I had to remove the -1 and +1 from the middleIndex adjustments before it worked for all numbers in the array. And interestingly, it takes the same number of iterations to find the number without adjusting the middleIndex by 1.

parkerlee
Автор

That's so cool. When I saw the 4 times looping in comparison to 100 loops. --> mind blown. Coding is the greatest hobby I have ever had.

TheAlderFalder
Автор

This is great stuff. Thanks Brian for making it easy to follow for people without an engineering background. I tried it and it even works with String, wow! Just make sure you sort the array of strings first.

bennychewDev
Автор

This guy is an amazing teacher! You’re truly great man! You made this look easy

cinquain
Автор

Love this series and all your swift videos! Thanks for the hard work for putting these together. I'm learning so much from your channel!

hyperyolo
Автор

Really like the way you explained this with such simplicity . Thanks

GG-hkiz
Автор

You and Sean Allen
Both are Great Teacher

pratikgupta
Автор

It’s so much easier learning a language when u already know another one haha I remember I had so much trouble learning JavaScript which now I see it as a peace of cake as well as Swift

jonathanhernandez
Автор

That was amazing, such logic, Thanks!

stepbystepscience
Автор

at the start of the while loop we have the middleValue, rightIndex, and leftIndex. might as well save more time by checking if array[rightIndex] or array[leftIndex] equals our searchValue. Instead of waiting for the middle value to slowly become our searchValue, we have 3 values that we can always compare once the loop reiterates :)


given this example, if our searchValue is 19, without this clause the loop will run 4 times. With this clause, it only runs twice. The likelihood of the array[rightIndex] or array[leftindex] becoming our searchValue increases over time as our chopped array gets smaller and smaller.

feleciacoleman
Автор

For linear search you can shortly use:

func linearSearch(searchValue: Int, array: [Int]) -> Bool {
if array.contains(searchValue) {
return true
} else {
return false
}
}

harungunes
Автор

It's very cool!!! Super interesting series! And your explanation is very easy to understand! I think it's a best channel about Swift and iOS programming. Thanks for you I learned a lot. Keep going! Looking forward new videos.

andreychirkov
Автор

The graph of log n is not correct. Hint: Think about where this graph should cross the x axis. For all relevant values of n, log n < n.

isaacclark
Автор

Hey, thanks for doing algorithm stuff in Swift. Have a couple questions.

on this line, let middleIndex = (leftIndex + rightIndex) / 2. Do all binary search implementations do it this way? leftIndex + rightIndex can produce an odd number. Do we just assume the decimal values are completely ignored in all implementations of binary search algorithms? or would it be proper to explicitly handle that case.

Also, some other popular algorithms you can do are, merge sort, breadth first search, depth first search. Thanks again for these awesome tutorials.

joa
Автор

That was a pretty good explanation on binary search....Keep it coming

razorx
Автор

Thanks for teaching master Brian. Regards from Perú :)

angel
Автор

Awesome video! Can we please get more algorithm videos because these are really helpful!!!

bcyourself
Автор

Great channel and great video!!
Thanks for all the content you bring us :)

jagonza