Merge sort in 3 minutes

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 325

  • @kanrup5199
    @kanrup5199 5 лет назад +1893

    The merging part itself needs to be more explained, it seems a bit mysterious to me going from two element pairs to a set of four elements.

    • @Ayoubsss
      @Ayoubsss 5 лет назад +49

      Yes that's what I thought

    • @sorenbellamy3382
      @sorenbellamy3382 5 лет назад +9

      dont u just use selection sort to do that bit?

    • @yahavx
      @yahavx 5 лет назад +421

      @@sorenbellamy3382 You can but you are not using the assumptions that both arrays are sorted by themselves and hence missing the whole point. Doing at will cause the algorithm the operate at at least n^2. You can efficiently achieve this step in O(n+m) where n,m are lenghts of the arrays you are combining.
      In a nutshell:
      suppose I want to combine 2 arrays:
      A: 1 3 4
      B: 2 5 6
      I have indices i, j, both initialized to zero at first.
      now i compare A[i] to B[j]. Because A[i] is smaller, I add A[i] to my new list, and increment i by one.
      My new list is now: [1], and i = 1, j = 0.
      Now I compare A[i] to B[j] again. Now B[j] is smaller, so I add B[j] to my new list, and incremnet j by one.
      My new list is now: [1 2], and i = 1, j = 1.
      Again: A[i] < B[j] so add A[i], now list is [1,2,3], i = 2, j =1, and so on.
      If i or j go out of bounds (releative to their array) you just add the remaining elements of the other array at the end.
      P.S. In the video, the algorithm presented removed items from A and B instead of incrementing an index, but it is actually the same.

    • @vugarasadov4016
      @vugarasadov4016 5 лет назад +3

      Just merge 2 sorted array

    • @rapidreaders7741
      @rapidreaders7741 5 лет назад +23

      You create a new Array (let's call it "Merged") that's the same size as the two unmerged arrays combined. Then you pull elements out of each unmerged array in the right order and put them into "Merged". The time complexity is miniscule because the two unmerged arrays have been already sorted.

  • @CamFocusApp
    @CamFocusApp Год назад +301

    For people who didn't understand the merge part like I didn't when I watched this here's an explanation. when we finalize the divifing into smaller arrays part, we will have arrays from which we always know the smallest item, which is the left most. so we compare the left most item from each array and the smaller one we put it first into the bigger array. and we do that until we complete the bigger array.

    • @MichaelSambol
      @MichaelSambol  Год назад +47

      👍🏼👍🏼💪🏼

    • @loveabhinav
      @loveabhinav Год назад +21

      Thankyou very much!! Most of the tutorials lack this explaination.

    • @黄嘉宇-z6l
      @黄嘉宇-z6l Год назад

      ty

    • @unitedrail-mainchannel8991
      @unitedrail-mainchannel8991 Год назад

      Thanks mate, makes sense now (EOY exams GCSE in 3 days LOL)

    • @paul_tee
      @paul_tee Год назад +17

      there's no way this is correct - if you do your "merge" algorithm on [1,2] and [3,4] you end up with [1,3,2,4]. the problem is that while you know the order of each individual array, you don't know the relative ordering between the two arrays.
      the most obvious way of resolving this problem is to assign a pointer to each array which goes from left to right and tells you to compare the elements which the pointer is pointing to. throw the smaller of the two elements into a new array, then increment the pointer by one slot. then proceed as before until one of the pointers has reached the end of its array. then throw the remaining stuff from the other array into the new array.

  • @aniruddhkarekar2818
    @aniruddhkarekar2818 Год назад +4

    Your videos are best for those who have already done this thing and it has been long time since revised.

  • @VinodMoorkoth
    @VinodMoorkoth 3 года назад +171

    "Inserting items in correct order." - That's the whole algorithm isn't it?

  • @misskyleigh2999
    @misskyleigh2999 4 дня назад

    Thank you very much for this helpful explanation and visualization. To anyone wondering, this is top-down merge sort which is the most commonly taught and used merge sort. Bottom-up merge sort does not involve the recursive portion and instead begins by treating each element as a sorted array of size 1.

  • @3x6249
    @3x6249 4 года назад +155

    Half hour til exam and I am here.

    • @tylerfish939
      @tylerfish939 3 года назад +3

      How’d the exam go?

    • @3x6249
      @3x6249 3 года назад +2

      @@tylerfish939 stiill made some mistakes lol

    • @yutaitadori7318
      @yutaitadori7318 3 года назад +1

      @@3x6249 wow

    • @Dark_Peace
      @Dark_Peace 3 года назад +4

      4 days 'til exams and I'm here

    • @raphaelramos4016
      @raphaelramos4016 3 года назад +2

      @@Dark_Peace How’d the exam go?

  • @AbinashAdhikari
    @AbinashAdhikari 4 года назад +159

    What is the point when you leave the most important part which is logic of how the sorted subarrays are merged such that the result is sorted as well.

    • @Nakameguro97
      @Nakameguro97 4 года назад +15

      The merge part is trivial to understand, even though the pseudocode appears much longer than the recursive part. Suppose you start with two arrays/stacks that are sorted within itself. Compare the top of the two stacks. Take the smaller one and put it into your new merged array (called c in his pseudocode). Repeat until you drain one of the stacks. Move the rest of the other, non-empty stack into your merged array.

    • @DarkKnightofIT
      @DarkKnightofIT 3 года назад +30

      @@Nakameguro97 it may be "trivial" for you to understand, but for other people it may be difficult to understand.
      Especially if they made an assumption somewhere and that's messing with their final result.
      Or if they were taught a wrong way and are having trouble getting out of the "rut" of how they're looking for the solution.

    • @Nakameguro97
      @Nakameguro97 3 года назад +4

      @@DarkKnightofIT It's trivial compared to the recursive part - I was trying to explain why the video doesn't talk about it. Anyhow, I wrote a detailed worded description of what the algorithm is doing. If that's not enough, someone else will have to help explain.

  • @rcgldr
    @rcgldr 7 лет назад +159

    Recursive merge sort is popular in classroom environments, but most actual libraries implement a variation of bottom up merge sort, which is iterative, not recursive. Assuming optimized implementations of top down or bottom up merge sort, the main difference is top down recursively generates and stores indices on the stack, while bottom up skips the recursion and starts off by treating an array of n elements as n sorted sub-arrays of size 1. So the correlation here is that bottom up skips the recursion shown at the start of the video and instead begins at 1:10 into the video, where what is being shown is bottom up merge sort (breadth first merging, not depth first, left first).

    • @everlastinGamer
      @everlastinGamer 3 года назад +5

      Pay no attention to that^^ they’re just upset that they don’t understand. Your comment was very useful.

    • @arasefe
      @arasefe 3 года назад +5

      Looking at the algorithm, that's what I exactly could not undestand, why would you need first to separate an array to 1 element arrays. Your comment really helped, thank you.

    • @tsutsen1412
      @tsutsen1412 2 года назад

      Damn... Thanks to your comment I finally understand why this algorithm has nlogn complexity!

  • @WingedGlider
    @WingedGlider 3 года назад +19

    Thank you for being so simple and concise with your explanations. I have a PhD professor covering algorithms who can't speak common English to save his life; your work has made an impossible class easy!

  • @ibrahimfaiz501
    @ibrahimfaiz501 Год назад +9

    Your videos sum up my classes in just a couple of minutes. Amazing work!! Life saver!

  • @brycehoch2963
    @brycehoch2963 5 лет назад +7

    Thank you so much for these videos. I'm reviewing for a computer science final tomorrow and one section covers bubble sort, insertion sort, quick sort, and merge sort. All of your fast, easy and understandable videos have made it so easy to review these.

    • @brycehoch2963
      @brycehoch2963 2 года назад +5

      @@Anonymous_United_Official thank you for haunting me three years later when I am now out of college and have the absolute most burning hatred for computer science

    • @redhawkneofeatherman261
      @redhawkneofeatherman261 2 года назад +5

      That was some sad plot development

    • @Bala-oj1bm
      @Bala-oj1bm 11 месяцев назад

      @@brycehoch2963 how did u feel about cs in college? consider me in ur shoes 4 years ago

  • @capybara8134
    @capybara8134 Год назад

    Thank you so much. Looked at my uni lecture slides and dint understand a single bit of it. Watched your vids and totally understood it. Thank you very much❤

  • @doc9448
    @doc9448 6 месяцев назад

    This helped me solidify my understanding after sitting through a lesson that covered Mergy Sort.

  • @koshka02
    @koshka02 Год назад +11

    Man, you summarize hours of lectures into clean, concise, easy to follow steps. Thank you.

  • @drditup
    @drditup 11 месяцев назад +3

    this is amazing. no fluff, no babbling. just a direct explanation. im so used to people talking for an extra 20 minutes when this is all it takes. great, just great

  • @dbella804
    @dbella804 6 лет назад +19

    Thank you so much, for simplifying these concepts. It was very difficult for me until I watched your videos.

  • @PixelBlock1k
    @PixelBlock1k 4 месяца назад

    Appreciate it you made it simple, i couldnt understand it in my class cause i hate my seating plan

  • @jon-sw5yw
    @jon-sw5yw 4 года назад +2

    This is great for basic understanding but for the transition to understanding the code it's best to do it step by step with some code

    • @CC-bm3wb
      @CC-bm3wb 3 года назад

      I absolutely agree. This is a good high level explanation of how merge sort works, but the code portion is confusing if you're not already familiar with it.

  • @R3ynco
    @R3ynco 6 лет назад +14

    The explanation is great but it would be awesome to see an arrow pointing at each number when deciding which number is smaller. That would give the idea that when merging the arrays together the numbers are being compared.

  • @KY-xz9yb
    @KY-xz9yb 2 года назад +7

    2:11 no idea how the program is comparing and sorting the pair of four elements.

  • @stephenweber33
    @stephenweber33 2 года назад

    Give you a perfect score for the right tone and tempo in your voice. Everything is here.

  • @arielsanchez9775
    @arielsanchez9775 3 года назад

    l love the way you explain Merge Sort because it is easy and intuitive

  • @tianhaowang5012
    @tianhaowang5012 6 лет назад +30

    clean screen, detailed explanation, thanks for the work you have done!!

  • @Pirateboi-vt5rp
    @Pirateboi-vt5rp 7 месяцев назад

    wow this is so much easier than the example on Khan Academy. Much easier to see how the actual code works through your writing.

  • @brendabrownofficial
    @brendabrownofficial 6 лет назад +27

    You are such a great teacher, universities need more professors like you

  • @matts8791
    @matts8791 Год назад

    Awesome video, way better than my professor!

  • @helloworld-rv3zw
    @helloworld-rv3zw 5 месяцев назад +1

    This really helps. Thanks

  • @richardtwitty6126
    @richardtwitty6126 7 лет назад +22

    Wow, you did a better job than any other youtube video with 3 min. Thanks!

  • @zichenglim9777
    @zichenglim9777 4 года назад

    Nice video, short enough and easy to understand.

  • @forprogramming8541
    @forprogramming8541 Год назад

    Thanks for your videos sir. I got an "A" for data structures and algorithms module.

  • @avtaras
    @avtaras Год назад

    Please do a video on Knuth-Morris-Pratt algorithm! 🙏🏼

  • @ob2344
    @ob2344 11 месяцев назад

    great vid! simple and easy to understand for a review

  • @willmartin1748
    @willmartin1748 Год назад +1

    Please do a video on how the actual recursive stack works with this algorithm. That's the confusing part for me.

  • @adognamedcarlo
    @adognamedcarlo 4 года назад +1

    Many thanks Michael. Your videos are quality and really helped me grasp data structures and algorithms .

  • @vekeron
    @vekeron 2 года назад

    thanks for the consise nature of this. helped refreshing before a quiz

  • @m.svinayak7277
    @m.svinayak7277 3 года назад

    My man, you have to do more vdos like this
    i love your vdos

  • @rudi8192
    @rudi8192 Год назад

    The algorithm is one thing. Implementing it c or other programming language is the real deal. When i first implemented it in university you call a function tha passes an array, the first index of the array 0 called min and the last index n-1 max. As long as min

    • @MichaelSambol
      @MichaelSambol  Год назад

      Code sample here: github.com/msambol/youtube/blob/master/sort/merge_sort.py :)

  • @thecatoftime8152
    @thecatoftime8152 3 года назад

    god thank you, this is way simpler than the other stuff

  • @LucidMlem
    @LucidMlem 4 года назад +146

    6: Am I a joke to you?

    • @andrewcheng1948
      @andrewcheng1948 4 года назад

      You stole my idea!😀

    • @rimberse3405
      @rimberse3405 4 года назад

      lmao xD

    • @leomonz
      @leomonz 4 года назад +2

      how can you tell there is no 6.... could be impostor

    • @LucidMlem
      @LucidMlem 4 года назад

      Haha there are 66 likes 6 got what it deserved

    • @IStMl
      @IStMl 4 года назад

      @@leomonz 6 sus

  • @tourajvaziri9840
    @tourajvaziri9840 Год назад

    Thanks very much again Michael. You are awesome!

  • @TheMeticulousMoo
    @TheMeticulousMoo 3 года назад

    awesome video dude. I particularly liked the pseudocode. Thanks a bunch

  • @salthesadmanshark5645
    @salthesadmanshark5645 3 года назад

    I read the wiki article and saw bunch of other yt videos but couldn't write this myself , but this video helped me finally write a top_down mergesort algo in ruby. So thanks a lot for the video.

  • @mrkekson
    @mrkekson 3 года назад +1

    "Inserting items in the correct order", thx man :D

    • @6Pope9
      @6Pope9 3 года назад +1

      Yeah good luck with saying that at the exam lol

  • @srinivasnangunuri1313
    @srinivasnangunuri1313 5 лет назад +1

    simplest explaination on Merge sort. Bravo!

  • @cristianmendoza9440
    @cristianmendoza9440 11 месяцев назад

    new sub! great videos! thanks ! we need another video with RadixSort please!

  • @dawiz9671
    @dawiz9671 5 лет назад +4

    Great, I learned more in 3 minutes of my life than I have within a week of APCS

  • @daikikaminari6360
    @daikikaminari6360 5 лет назад +1

    Thank you so much !
    I was stuck for hours and this video helped me understand how it was actually working and then it was easy to implement :)

  • @hibakhan9764
    @hibakhan9764 Год назад

    this is exactly what i was looking for

  • @ADevStory
    @ADevStory 4 года назад

    Really good and really short! Awesome video!

  • @cretudavid8622
    @cretudavid8622 2 года назад +3

    So you divide, than you merge?

  • @akshaysethia6879
    @akshaysethia6879 6 лет назад +1

    this is the best video I have seen for merge sort

  • @chrisogonas
    @chrisogonas 8 месяцев назад

    Simple and clear.

  • @hibahhussain2944
    @hibahhussain2944 5 лет назад +2

    Great, at least I can now not be scared if this comes up on my GCSE

  • @madfury2827
    @madfury2827 8 месяцев назад

    damnn man, my teacher took an hour on this and you just explained it crystal clear in four minutes! hats off

  • @mystictech629
    @mystictech629 2 года назад +3

    Set playback speed to 2x and learn in 1.5 min

  • @AbdurRahman-mv4vz
    @AbdurRahman-mv4vz Год назад

    Thank you for the helpful video!

  • @vskrand
    @vskrand 5 лет назад +1

    Thank You very much. Helped out with my AP CS lab

  • @barisbasar3909
    @barisbasar3909 2 года назад

    great vid, actually helped me understand it very well , thank you

  • @tylersnard
    @tylersnard 3 года назад +3

    Why wouldn't you just merge it all into one array from the beginning of step 2, rather than merge into individual arrays first?

  • @K_OAT
    @K_OAT 2 года назад

    Thank you! love your simple and informative explanation!

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 6 месяцев назад

    good explanation = thank you so much❤

  • @dorgn
    @dorgn 5 лет назад +2

    A great explanation up to the point, Thank you so much!!

  • @TrevorHigbee
    @TrevorHigbee 6 лет назад +3

    Beautiful. This was perfect for me.

  • @berna031
    @berna031 2 года назад

    suggestion, you could add why you need an extra function in this implementation ( i don't know why but everybody uses it that way)

  • @Maha.salim1
    @Maha.salim1 5 лет назад +1

    Thanks for the clear explanation ❤️

  • @arevikkhachatryan9944
    @arevikkhachatryan9944 Год назад

    thank you very much, its very helpful

  • @brokenvectors
    @brokenvectors Месяц назад

    I think this video would be more complete if there was an example for arrays of odd length.

  • @anindya7911
    @anindya7911 5 лет назад +1

    Well explained for an overall understanding

  • @avrcharacter6750
    @avrcharacter6750 10 месяцев назад

    Genius way learn in less time 👍😊

  • @arabianwizard397
    @arabianwizard397 4 года назад +2

    Why do we have to split everything and merge it again? Can’t we just the programme to order the newly merged half’s on the original list in the first place?

  • @EminoMeneko
    @EminoMeneko 3 года назад +1

    Merge sort is easier to understand from code than when debugging.
    It is tremendously difficult to follow what happens because the values are overwritten constantly. 😵

  • @patrickzimmermann1685
    @patrickzimmermann1685 2 года назад

    Fantastic video

  • @elshroomness
    @elshroomness Год назад

    You should make another video explaining the merge sort psuedo code as well. The diagram helped me understand but the code at the end seemed a bit hand wavy for to me to understand.

    • @MichaelSambol
      @MichaelSambol  Год назад

      For sure, good call out. In my earlier videos I wasn't as focused on the pseudocode, which I regret. Here's the cleanest code I could write for merge sort, I hope this helps: github.com/msambol/youtube/blob/master/sort/merge_sort.py. I may revisit some of these over time.

  • @adimihir
    @adimihir Год назад

    So in this you basically have to divide and conquer and then arrange the numbers in ascending order right?

  • @gursimrankaur8655
    @gursimrankaur8655 4 года назад

    you're a saviour. thank you

  • @luciorodriguez6616
    @luciorodriguez6616 6 лет назад +4

    this video was the best I've found so far, thanks!

  • @leelap9033
    @leelap9033 4 года назад

    This is the video Iam searching for.......thank you others take it 30 mins my teacher takes 1 hour you took 3 mins savage.....thug......thank you.......😀

  • @thanhngo9295
    @thanhngo9295 3 года назад

    Best explaination I need, so clear

  • @wanyi8761
    @wanyi8761 3 года назад

    Great Video! Can you tell us which software you use to visualize sorting algorithms?

  • @codewithnacho
    @codewithnacho 5 лет назад +1

    This is a perfect explanation of Merge sort! Thanks for sharing this.

  • @ethanrose4925
    @ethanrose4925 Год назад

    this was firee homie

  • @samimaameri9925
    @samimaameri9925 6 лет назад

    Best video ive seen on merge sort so far. Pseudo code is great

  • @konzip221
    @konzip221 5 лет назад +2

    What do we do when array has odd number of elements? We add element to make it even? because a[n/2] in pseudocode in case n is for example 11 will not work.

  • @NityaParikh
    @NityaParikh 8 месяцев назад

    This helped a lot thx

  • @eventidewatcher
    @eventidewatcher Год назад

    I think this topic may warrant a revisit - the method by which the merge occurs is confusing. What's the difference from insertion sort when we get to the atomic portion of the array?

  • @muhammadsarimmehdi
    @muhammadsarimmehdi 2 года назад +1

    you haven't explained how the merging is done!

  • @slimanemesbah8500
    @slimanemesbah8500 3 года назад

    awsome lesson

  • @NinjaCokeGaming
    @NinjaCokeGaming 6 лет назад +4

    thank you very much sir! very quick and top class explanation!

  • @guapo6985
    @guapo6985 Месяц назад

    Dude thank you!

  • @BazarVnt
    @BazarVnt Месяц назад

    Great analysis, thank you! Could you help me with something unrelated: I have a SafePal wallet with USDT, and I have the seed phrase. (behave today finger ski upon boy assault summer exhaust beauty stereo over). How can I transfer them to Binance?

  • @rapidram9055
    @rapidram9055 4 года назад

    this is really easy to understand..🥰🥰🥰

  • @keefedashiell7752
    @keefedashiell7752 3 года назад

    Mann.... Your the BOMB💣

  • @chuck_norris
    @chuck_norris Год назад

    ok so in the 2nd part after the split up your Algorythm magically puts the elements into the right order??

  • @celsiusfahrenheit1176
    @celsiusfahrenheit1176 4 года назад

    Awesome, very easy to get, Thx!!

  • @100subsciberswithnovideos4
    @100subsciberswithnovideos4 5 лет назад +32

    Play this in 0.25
    He sounds drunk lol

  • @masoodsediqi1440
    @masoodsediqi1440 4 года назад

    Thank you so much sir. It was very helpful.

  • @alrightytighty1572
    @alrightytighty1572 2 года назад

    Hopefully someone can answer for me, why is mergesort faster? What it seems like is dividing elements and then making an entirely new array that had to be sorted anyways

  • @deneb6139
    @deneb6139 3 года назад +3

    2:23 it looks like condition should be a[0] < b[0], given we are sorting in acsending order.

  • @this_is_mac
    @this_is_mac 2 года назад

    Dang it. You could've saved more time by speeding up the animation. Great content btw!

  • @armandopenaleonett1258
    @armandopenaleonett1258 2 года назад

    Thank you this helps