Merge Sort - Swift Tutorial - iOS Interview Coding Challenge

preview_player
Показать описание
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.

Thomas Hanning Blog Post about Merge Sort:

Harvard CS50 Merge Sort:

Xcode Project Source Code:

iOS Dev Courses:

Twitter:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Paul Hudson's Hacking With Swift:

Donny Wals - Combine:

Mark Moeyken’s SwiftUI Books:

Ray Wenderlich Books:

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

removeFirst is increasing time complexity, better to use temp variable of indexes

smritibharti
Автор

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!

alphonsosensleyii
Автор

Need help understanding Merge Sort in Swift? Leave a comment, I'm happy to help!

seanallen
Автор

Love these videos. Been teaching myself swift and I cannot stress enough how useful these are.

crossfitbeast
Автор

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.

jenna
Автор

best mergesort implementation i have ever seen. thannks man

mohsinkhan
Автор

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

demolition
Автор

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", "\n") instead of just having another print func

YoelEphotography
Автор

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.

joa
Автор

good video Sean, ill have a go at a different approach at the weekend when i'm feeling better

colonelkob
Автор

393 likes and 0 dislikes
this is the power of sean
by the way great video

pratikgupta
Автор

Very clear explanation of mergesort. Nice! Have you been asked to do insertion sort, heap sort, or selection sort in an interview?

isaacclark
Автор

Nice vid and very nice explanation but I didn't understand why is the return part of mergeSort recursive / repeating

protaotogamer
Автор

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!

preetamjadakar
Автор

Great video, great explanation! Thank you!

okolo_food
Автор

Thank you for these swift videos, it is helping me learn swift immensely. I was curious if there is any issue using <= instead of < on line 25 to make this a stable sort?

Rousen
Автор

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?

vigneshrajsb
Автор

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?

olegk
Автор

Nice tutorial Sean.
What is the algorithm used in swift default sort function?

guruteja
Автор

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...????

vamsikamjula