Merge Sort - Swift Tutorial - iOS Interview Coding Challenge
Вставка
- Опубліковано 21 жов 2024
- The next video in my iOS Interview questions series all about coding challenges explains a common sorting algorithm called Merge Sort in Swift. I have been asked to code this during a phone screen interview for an iOS position, so it's good to know for a variety of reasons.
iOS Dev Courses - seanallen.teac...
Thomas Hanning Blog Post about Merge Sort:
www.thomashanni...
Harvard CS50 Merge Sort:
• Merge Sort
Xcode Project Source Code:
www.dropbox.co...
iOS Dev Courses:
seanallen.teac...
Twitter:
Sean Allen - / seanallen_dev
Hired.com:
hired.com/x/1n01g
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Paul Hudson's Hacking With Swift:
gumroad.com/a/...
Donny Wals - Combine:
gumroad.com/a/...
Mark Moeyken’s SwiftUI Books:
www.bigmountainstudio.com/swiftui-views-book/fzc51
Objc.io Books (Thinking in SwiftUI & Advanced Swift):
gumroad.com/a/...
Ray Wenderlich Books:
store.raywende...
#swift #softwaredeveloper #iosdeveloper
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ua-cam.com/video/MSIe2y6Fee8/v-deo.html
Need help understanding Merge Sort in Swift? Leave a comment, I'm happy to help!
Nice video Sean, really helpful! Could you do one on Quicksort? That algorithm has always confused me.
+Matthew Guest Hey Matthew, glad you enjoyed the video. Quicksort is on the list, so I'll get to it eventually. So many videos to make, so little time, lol.
removeFirst is increasing time complexity, better to use temp variable of indexes
Nice!! Sean, you are the man! I am a little over a year into my iOS Dev journey and your videos really encourage me to keep pushing. I have yet to come across any other online tutorials that give such in depth content while still being easy to understand. Pretty sure if you created a book it would sell like crazy. Anyway, Hope you are enjoying the new gig! and thanks for sharing your knowledge!
Thanks Alphonso! I appreciate the kind words and glad you're enjoying the videos. The new gig is going great so far 👍
Love these videos. Been teaching myself swift and I cannot stress enough how useful these are.
Glad the videos are helping, Don!
best mergesort implementation i have ever seen. thannks man
Wish I watched this before my interview, never heard of a merge sort before then and ran out of time before I finished my function
Now you're ready for it next time :)
good video Sean, ill have a go at a different approach at the weekend when i'm feeling better
Glad you enjoyed it, Ben!
Fun question, normally I expect merge sort to retain order. For example if you're sorting objects by some calculated hash value, you might want the leftmost object to stay on the left when hashes are equal. To do that with your example, shouldn't the line comparing left.first and right.first be "less than or equal to", rather than "less than"? That way, merge would first append from the left and then append from the right on the next iteration.
Hi Allen,
Love those algorithm tutorials, you are straight to the point adding a nice recap at the end.
BTW you can print("Your text","
") instead of just having another print func
Glad you enjoyed the video, Yoel! And thanks for the tip 😀
Great video, great explanation! Thank you!
Happy to help!
I know this is getting too specific, but I would assume implementing merge sort, we care about run time. Utilizing removeFirst itself has a runtime of O(n). Your merge sort implementation with the removeFirst method will cause your runtime to be exponential.
393 likes and 0 dislikes
this is the power of sean
by the way great video
Very clear explanation of mergesort. Nice! Have you been asked to do insertion sort, heap sort, or selection sort in an interview?
Hey Isaac, I have not been asked about those others, but I have seen Quick Sort and Bucket Sort show up on a list of "Things you may be asked about" by a company recruiter in preparation for final interviews. However, they weren't asked in that specific interview.
Thanks Mr. Allen. God luck with your job search.
Nice vid and very nice explanation but I didn't understand why is the return part of mergeSort recursive / repeating
Well explained thank you
one more optimization in addition to removeFirst() method as it involves shifting, what if elements are the same? this algo is still changing the order instead of keeping as is! I think this the beauty of Merge Sort!
In other words: Merge sort is stable but you implemented it as unstable.
Need to add equal to case in while loop. Please!
Thank you for these swift videos, it is helping me learn swift immensely. I was curious if there is any issue using
It doesn't matter either way. The case where it is equal will only come up when there is the same number in both the left and right array. And in this case it doesn't matter which duplicate you add to the mergedArray first. Either way will work. (Using just < or
Thanks for video Sean! Really good and useful one, as usual. But there is already exist build in sort array method called array.sorted(by:) Is it working slower or depend on cases some methods working better and need to write them by yourself? And if yes, in which cases each method working better?
+Олег Куплин You're right, there is a sorted() function in Swift. These coding challenges are the types of questions you'll see in interviews that'd you'll either have to code on a whiteboard or during a phone screen. It's not really meant to replace he actual sort() method. These are just coding exercises you'll see in interviews.
Sean Allen got it Sean. Thanks for what you’re doing. Wish you more subscribers in future and career growth! Enjoying every your video
+Олег Куплин thanks for the kind words! The channel is only 3.5 months old, and I'm having fun with it. More videos to come!
Nice tutorial Sean.
What is the algorithm used in swift default sort function?
Not sure off the top of my head, but here's a link to a StackOverflow post discussing it. stackoverflow.com/questions/41031106/which-general-purpose-sorting-algorithm-does-swift-use-it-does-not-perform-well/41070802
Hello Sean, great videos !!!!!
We can also do sorting in simple way by using following lines of code...
var numbers = [9, 8, 7, 6, 3, 2, 1, 4, 5]
numbers = numbers.sorted(by: {
$0 < $1
})
print(numbers)
Is this recommended or we have to do a recursion in whiteboard challenge...????
For sure, there are shorter ways to do my "Coding Challenge" videos, however when you're doing a whiteboard interview, they typically want you to do it the long way. Anyone can just type .sorted to sort something, but the point of the interview is to see how you think, and how you would solve to problem. It's not so much about getting a result. It's how HOW you got it.
Thanks !!!!
My brain is like 🤯
Excellent tutorial 😃😃
Glad you liked it, Ahaan!
I tried to print the merged array and the left & right arrays every time the function is called. looks like the loop runs through twice. Can you please tell me why this is happening?
After sorting I got this result result : [5, 4, 2, 6, 7, 3, 1, 8] while I have followed your steps.
Did you try to download the source code from the description, and compare your code with mine?
I got it now. I should always watch the whole video instead of just looking at the code.
No worries 😀
How to deal with High quality images inside only one image for all sizes of devices iPhone and iPad what kind of images we need to use?
the problem is that our code is not working Sean.
i love this!!
Thanks for making this tutorial and also thanks for recommending awesome "THOMAS HANNING" website.
Happy to help!
What algorithm does in-built iOS sort or sortBy function uses? what is the time and space complexity?
I had to look this up. You can find more info and links here: stackoverflow.com/questions/41031106/which-general-purpose-sorting-algorithm-does-swift-use-it-does-not-perform-well/41070802
Thank you so much. I would like to interact with you about iOS. I figured out a way of having mock interview using www.pramp.com/ . I guess this will be mutually beneficial and solve our interaction purpose as well. I am an iOS dev from India.
www.pramp.com/invt/yBrKN5aMQ5sozbG6BNjV
Attempt this and does not work for me -> even copied and paste you code into a playground
sorry a mistake on my end with naming convention
nice...
About elements and indexes, 🤦♂️ my bad. I didn’t notice “
Doesn’t work anyway. I will send a like to my github. I do want to figure it out.
Sir firebase tutorial I'm waiting for it
I never use Firebase, so that probably won't happen for a while.
you are near 2k
+Yogesh Jaiswal It's rapidly approaching...
Great videos, but you're little bit too fast. And btw, you look like Tinkernut (ua-cam.com/users/gigafide).
Thanks Badhan, I'm working on slowing down a bit.