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

КОМЕНТАРІ • 67

  • @seanallen
    @seanallen  4 роки тому +2

    Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ua-cam.com/video/MSIe2y6Fee8/v-deo.html

  • @seanallen
    @seanallen  7 років тому +3

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

    • @matthewguest8606
      @matthewguest8606 7 років тому +1

      Nice video Sean, really helpful! Could you do one on Quicksort? That algorithm has always confused me.

    • @seanallen
      @seanallen  7 років тому +2

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

  • @smritibharti1930
    @smritibharti1930 4 роки тому +2

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

  • @alphonsosensleyii7630
    @alphonsosensleyii7630 6 років тому +3

    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!

    • @seanallen
      @seanallen  6 років тому

      Thanks Alphonso! I appreciate the kind words and glad you're enjoying the videos. The new gig is going great so far 👍

  • @crossfitbeast805
    @crossfitbeast805 7 років тому +1

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

    • @seanallen
      @seanallen  7 років тому

      Glad the videos are helping, Don!

  • @mohsinkhan845
    @mohsinkhan845 3 роки тому

    best mergesort implementation i have ever seen. thannks man

  • @demolition3612
    @demolition3612 2 роки тому +1

    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

    • @seanallen
      @seanallen  2 роки тому

      Now you're ready for it next time :)

  • @colonelkob
    @colonelkob 7 років тому +1

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

    • @seanallen
      @seanallen  7 років тому

      Glad you enjoyed it, Ben!

  • @jenna6132
    @jenna6132 6 років тому

    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.

  • @YoelEphotography
    @YoelEphotography 7 років тому +1

    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

    • @seanallen
      @seanallen  7 років тому +1

      Glad you enjoyed the video, Yoel! And thanks for the tip 😀

  • @smael1989
    @smael1989 5 років тому +2

    Great video, great explanation! Thank you!

  • @jo1717a
    @jo1717a 7 років тому

    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.

  • @pratikgupta7373
    @pratikgupta7373 3 роки тому +1

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

  • @isaacclark9825
    @isaacclark9825 7 років тому +1

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

    • @seanallen
      @seanallen  7 років тому

      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.

    • @isaacclark9825
      @isaacclark9825 7 років тому +1

      Thanks Mr. Allen. God luck with your job search.

  • @protaotogamer1503
    @protaotogamer1503 4 роки тому

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

  • @vaishnaviyerpude29
    @vaishnaviyerpude29 Рік тому

    Well explained thank you

  • @preetamjadakar3764
    @preetamjadakar3764 4 роки тому

    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!

  • @Rousen90
    @Rousen90 7 років тому +1

    Thank you for these swift videos, it is helping me learn swift immensely. I was curious if there is any issue using

    • @seanallen
      @seanallen  7 років тому

      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

  • @olegk5408
    @olegk5408 7 років тому +1

    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?

    • @seanallen
      @seanallen  7 років тому +1

      +Олег Куплин 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.

    • @olegk5408
      @olegk5408 7 років тому +1

      Sean Allen got it Sean. Thanks for what you’re doing. Wish you more subscribers in future and career growth! Enjoying every your video

    • @seanallen
      @seanallen  7 років тому

      +Олег Куплин thanks for the kind words! The channel is only 3.5 months old, and I'm having fun with it. More videos to come!

  • @guruteja7151
    @guruteja7151 6 років тому

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

    • @seanallen
      @seanallen  6 років тому

      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

  • @vamsikamjula7335
    @vamsikamjula7335 6 років тому +1

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

    • @seanallen
      @seanallen  6 років тому +1

      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.

    • @vamsikamjula7335
      @vamsikamjula7335 6 років тому +1

      Thanks !!!!

  • @donathmm3881
    @donathmm3881 4 роки тому +2

    My brain is like 🤯

  • @protaotogamer1503
    @protaotogamer1503 4 роки тому

    Excellent tutorial 😃😃

    • @seanallen
      @seanallen  4 роки тому

      Glad you liked it, Ahaan!

  • @vigneshrajsb
    @vigneshrajsb 6 років тому

    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?

  • @ombhagwan859
    @ombhagwan859 6 років тому +3

    After sorting I got this result result : [5, 4, 2, 6, 7, 3, 1, 8] while I have followed your steps.

    • @seanallen
      @seanallen  6 років тому +1

      Did you try to download the source code from the description, and compare your code with mine?

  • @Денис-ж3ф5р
    @Денис-ж3ф5р 3 роки тому

    I got it now. I should always watch the whole video instead of just looking at the code.

  • @anandnimje5750
    @anandnimje5750 7 років тому

    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?

  • @iOS_Developer_Swift
    @iOS_Developer_Swift 3 роки тому

    the problem is that our code is not working Sean.

  • @laurapotter6321
    @laurapotter6321 6 років тому +1

    i love this!!

  • @starecho0105
    @starecho0105 6 років тому +1

    Thanks for making this tutorial and also thanks for recommending awesome "THOMAS HANNING" website.

  • @vengateshkr
    @vengateshkr 6 років тому +1

    What algorithm does in-built iOS sort or sortBy function uses? what is the time and space complexity?

    • @seanallen
      @seanallen  6 років тому

      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

    • @vengateshkr
      @vengateshkr 6 років тому

      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

  • @berkat07
    @berkat07 3 роки тому

    Attempt this and does not work for me -> even copied and paste you code into a playground

    • @berkat07
      @berkat07 3 роки тому

      sorry a mistake on my end with naming convention

  • @mdrashidiqubal1188
    @mdrashidiqubal1188 6 років тому +1

    nice...

  • @Денис-ж3ф5р
    @Денис-ж3ф5р 3 роки тому

    About elements and indexes, 🤦‍♂️ my bad. I didn’t notice “

    • @Денис-ж3ф5р
      @Денис-ж3ф5р 3 роки тому

      Doesn’t work anyway. I will send a like to my github. I do want to figure it out.

  • @MortalKombatant
    @MortalKombatant 6 років тому

    Sir firebase tutorial I'm waiting for it

    • @seanallen
      @seanallen  6 років тому +1

      I never use Firebase, so that probably won't happen for a while.

  • @jaiswalji1
    @jaiswalji1 7 років тому +1

    you are near 2k

    • @seanallen
      @seanallen  7 років тому

      +Yogesh Jaiswal It's rapidly approaching...

  • @lulaputta
    @lulaputta 7 років тому +1

    Great videos, but you're little bit too fast. And btw, you look like Tinkernut (ua-cam.com/users/gigafide).

    • @seanallen
      @seanallen  7 років тому

      Thanks Badhan, I'm working on slowing down a bit.