Merge Sort In Python Explained (With Example And Code)

Поділитися
Вставка
  • Опубліковано 14 січ 2025

КОМЕНТАРІ • 201

  • @Eduardo_C
    @Eduardo_C 2 роки тому +53

    Probably the best videos I have seen on these basic algorithms on UA-cam. Keep up the awesome work! have learned so much from your videos!

  • @t-distributedkid3825
    @t-distributedkid3825 Рік тому +13

    Probably the best merge sort video on youtube....
    Lot of people just confuse on this topic due to lack of clarity... even the experieenced ones

    • @a.m.4154
      @a.m.4154 11 місяців тому +2

      The algorithm in code form is not intuitive or trivial at all.

  • @EagerEggplant
    @EagerEggplant 2 роки тому +7

    Been on YT for too long looking for exactly this, a simple implementation. Brilliant.

  • @shravani.a
    @shravani.a Місяць тому +1

    Your teaching method is so calm, it helps slow down my panic-stricken heart every time I start to code. Thanks!

  • @sucraloss
    @sucraloss 2 роки тому +38

    Wonderful explanation, I really like how you describe WHY you are doing things at each step.
    Many people create tutorial videos where they simply say "okay and now we do this to get it to compare how we want to" and don't say why they do it that way.

  • @anall3l3
    @anall3l3 9 місяців тому +3

    Clear, concise, thorough and simple algorithm. Finally understood this, thank you for your great explanation!

  • @Kamil-rf5qn
    @Kamil-rf5qn 11 місяців тому +2

    Hats off to you for explaining everything step by step, even the basic things like colon, every index. I'm a beginner with 3 weeks into python and i started my second course on FreeCodeCamp that's an interactive one with a lot harder projects than i ever done. This is my second recursion project, their variable naming was way too long and i was being overwhelmed with very long variable names. My code from project looks identical to yours and yet yours is so concise and easy to read. Recursion is still something that i'm struggling with, but i feel like this video got me closer to understanding it. Thank you!

  • @tusharnaagar2587
    @tusharnaagar2587 Рік тому +1

    read a comment that this is the best video on youtube for merge sort and i would like to say indeed it is the best

  • @RootUser-lg1qh
    @RootUser-lg1qh Рік тому +1

    Its my first algorithm in my life and I am finally able to code it.
    Thank you for your help.

  • @user-oz4ts5mv5h
    @user-oz4ts5mv5h 3 роки тому +8

    The most clearly explained one. Great job! Thanks!

  • @arnavverma2135
    @arnavverma2135 3 роки тому +26

    This was amazing, keep it up...

  • @waaaaaaaayyyyyynnnee
    @waaaaaaaayyyyyynnnee 3 роки тому +14

    This is an awesome video!
    I am doing the algorithms module on Khan Academy. I understood what i was suppose to do fairly easily. Khan Academy's challenge is in JS but was not understandable.
    I skipped through to the code example part and you explained this perfectly. I understood exactly why you were doing each line of code instantly.
    Putting this into practice may be a little harder but now i have a code example which is thoroughly commented in my own words to help me solve future problems using merge sort
    THANK YOU

  • @RoMAndrix
    @RoMAndrix 2 роки тому +7

    Nice video Felix! I'm subscribed now. Keep doing tutorials because you're well-structured in explanation. Thank you!

  • @ozzy4738
    @ozzy4738 2 роки тому +2

    This is really simple and elegant. I wish I found this video before battling with merge sort - Thanks

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

    thank you so much this is what I call an explanation going straight forward to the point with all the important details this is a worth millions views

  • @gouravtagore17
    @gouravtagore17 2 місяці тому

    damnnn been struggling with this one
    this is the best video one could possibly produce
    Thanks mate

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

    explanied so nicely ...understood after watching this vedio only.. mission accomplished!!

  • @aj_hari_7
    @aj_hari_7 10 місяців тому

    Best videos should reach more people. UA-cam should show this kind of videos in recommendations.
    Highly recommended. Superb Explanation.

  • @olafschlammbeutel
    @olafschlammbeutel 4 роки тому +7

    Exactly what I need right now

  • @julian7934
    @julian7934 Рік тому +7

    You explained this in such an easy way compared to my uni professor lol. I couldn't thank you enough for these vids!

  • @mohammedhrima1654
    @mohammedhrima1654 Рік тому +1

    thank you so much, You saved me, I tried to implement merge sort with C, but most of C tutorials make it complicated

  • @shivenmehru1571
    @shivenmehru1571 2 роки тому +3

    Great explanation with a simple and clean code example. Appreciate the help!

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

    This is the best explanation, I ever found on sorting . Thankyou sir!!

  • @TonyBaloney-c4k
    @TonyBaloney-c4k Рік тому +2

    Great explanation. Remember to add the base case when len(arr)

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

    Much better than the million videos out there just talking rubbish

  • @rhodabaruch4
    @rhodabaruch4 Рік тому +1

    Thank you! This was a great in-place solution. I would love to see you walk through a non-in-place solution.

  • @mythbuster4315
    @mythbuster4315 Рік тому +1

    excellent tutorial! right before my exam!

  • @ManuelGozzi
    @ManuelGozzi 6 місяців тому +1

    Very well explained. My algorithms class was terrible lol.

  • @pika3.14
    @pika3.14 Місяць тому

    One of the simplest explanations for Mergesort. Thank you

  • @DungNgo-lw5lf
    @DungNgo-lw5lf 5 місяців тому +1

    finally understand, best explanation

  • @maxfrischdev
    @maxfrischdev Рік тому +1

    Great explantion! Toll gemacht! 🙂 Danke Felix

  • @SI-vk9pu
    @SI-vk9pu 3 місяці тому

    Hi, thanks for the great explanation. I would just add one thing, although you explicitly mentioned the case of comparing equal numbers at 10:39, using the "less or equal" operator in line 15 instead of the "less" operator would make the algorithm stable. As an example, let's say we come across the values 3 and 3 in the left and right vector, respectively. A stable algorithm would put the 3 from the left vector first, which the algorithm in the video does not.

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

    Bro u r amazing at explaining these! Best so far that I have found.

  • @mpsmps2151
    @mpsmps2151 2 місяці тому

    Best video ever I seen .Thank you 🙏

  • @kieseatic9983
    @kieseatic9983 10 місяців тому

    The explanation was so clean and easy to understand... Thank you 😄

  • @DanielSmith-uj7rr
    @DanielSmith-uj7rr 3 роки тому +1

    Hey There! Thank you very much for explaining this algorithm. You made it so easy to understand. I appreciate it. Thank you!

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

    you did an absolutely amazing job on explaining merge sort! im glad that i found ur vid!

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

    Great video, thanks! It really helped me understand it. The way of splitting the lists was great, I hadn't thought of that
    Grüße

  • @lena-ck9wz
    @lena-ck9wz 4 роки тому +4

    i really like your videos

  • @adelaidekhayon2631
    @adelaidekhayon2631 3 місяці тому

    Thank you, the best guide ever

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

    The best video of all the videos!

  • @VladislavRashkov-e6w
    @VladislavRashkov-e6w 5 місяців тому

    Fantastic! Thank you so much for the detailed explanation!

  • @EsotericEchoesTailored
    @EsotericEchoesTailored 11 місяців тому

    Great explanation and amazing presentation also! Thank you so much for sharing the knowledge!

  • @TheKorkahn
    @TheKorkahn 4 роки тому +4

    I love sorting numbers! Thank you for the great tutorial

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

    So informative! Big up from jp🇯🇵

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

    yeah really its very simple to understand the concept thanks dude
    for me before reaching to this video i think that merge sort is very hard to understand but when i saw this video i can say its very very simple
    thank you so much dude😊😊

  • @SanjuKumar-hk8yy
    @SanjuKumar-hk8yy 3 роки тому +2

    Well done, dude your explanation is very clear I understand your explanation. Keep upload more video on DSA. 🤘🤘please

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

    best explanation yet thanks.

  • @venkatabora1459
    @venkatabora1459 3 роки тому +7

    Thanks for the clear explanation.But i have few questions. When we are calling the merged array(left_arr) function if my array is of length 1 for example. My code checks for the condition whether the length of my array is greater than 1 or not ie..if(len(arr)>1 and it will fail. When will it go to the first while conditions. Ex: 2,6,5,1,7,4,3 and my left_list=2 and right_lst=6 and now it will run array(left_arr) ie array(2) the condition is not satisfied here since my array length is equal to 1 so it will not go inside the loop right? Correct me if i am wrong since i am new to programming

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

      This is when the recursion will stop with the list values of 2 and 6 and the lines of code after the recursive calls will be executed.

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

    Excellent explanation of the code. Super easy to understand the concept this way, thank you.

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

    that was awsome, you've helped me so much

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

    You are awesome for teaching like this. Thank you a lot

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

    Sirrrr wonderfully explained.
    just loved it.

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

    You explain things so well, thank you!!!

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

    Thanks for the video. However, I don't understand couple of things: 1) how come the code ever get to the line which starts with # merge comment if it is always sending array to the recursion in the case of len(array)>1 and if that condition is not satisfied it just returns the array (actually not because there is no return) so #merged comment and the rest of the code is never going to be executed. 2) Also, as I mentioned in the parantheses before, it returns "None" if I apply this code and if I add the return command (return array) at the end of the function, it gives wrong answer in which the array is not sorted correctly.

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

      I have the same doubts! Did you sort them out?

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

      @@gabrielvictorrusso5931 did you try it?

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

      Yes, I did, I know it works wonderfully, but I need help to understsnd whats happening, otherwise I’m just learning code recipes

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

      @@MahlakaSami Same issues.

    • @paulster185
      @paulster185 Рік тому +4

      You're correct that the code only enters the merge section (# merge comment) when the length of the array is greater than 1, and it recursively calls merge_sort on the left and right halves of the array. However, the merge section is executed during the backtracking phase of the recursive calls.
      When the recursive calls start to return, they bring back smaller sorted subarrays. The merge section is responsible for combining these smaller sorted subarrays into a larger sorted array. So, the merge section is indeed executed during the recursive backtracking phase, not during the initial recursive calls. This is why the code is able to sort the array even though it might not seem immediately obvious.
      Regarding the issue with returning "None":
      In the provided code, the sorting is achieved by directly modifying the input array arr. Instead of returning the array, the sorting is done in-place, and the final sorted array is available directly in the arr variable.
      The original code sorts the array in-place, so there's no need to add a return statement at the end of the function.
      I hope this explanation helps :)

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

    Thanks, the explanation helped me a lot.

  • @osyman782
    @osyman782 2 роки тому +3

    Why does the function have no return

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

    love ur lucid explanation❤❤❤❤❤

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

    thank you, I finally understood the merging step.

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

    Very Easy to learn. Thanks

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

    Was very helpful in the last minute

  • @luvlifk
    @luvlifk 10 місяців тому

    very nicely explained merge sort.

  • @Nick-gs4em
    @Nick-gs4em 3 роки тому

    Absolutely amazing. Thank you, thank you, thank you.

  • @vihangatharushan4477
    @vihangatharushan4477 10 місяців тому

    actually this is very simple and wow explanation

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

    brilliant explanation, Hats off

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

    great video with details and simplicity, thank you!

  • @charliewebb7244
    @charliewebb7244 7 місяців тому +1

    perfect video. 10/10

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

    Thank you, this was a super useful video!

  • @atheeshrathnaweera
    @atheeshrathnaweera 5 місяців тому

    Great explanation. Thanks.

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

    Neat clean and concise

  • @ayushshubhankar858
    @ayushshubhankar858 10 місяців тому

    Thanks for these videos felix!!

  • @savewater2840
    @savewater2840 6 днів тому

    At 9:50, in line 15, adding an equality to the if condition ensures that the the sorting algorithm is stable too...

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

    I Loved It So Much.. Good Luck

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

    You've done more for mankind then democracy ever has.

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

    thank you! very easy to understand explanation

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

    Very clear! Thank you very much

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

    Cool realization!

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

    Simply Beautiful, thank you.

  • @mohsinkhattak2840
    @mohsinkhattak2840 2 роки тому +2

    hey felix i'm abit confused regarding the last two while loops in which we are simply adding the remaining elements of either of the array if there are! how are we checking those remaining values for comparison?

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

      This is the case where all the elements in the other array have already been added to the sorted array. If your left array is [1,2,4,5] and your right array is [2,3,1828,183885], then by the time you reach 1828 in the right array, all the elements in the left array will have already been sorted in the main array, so you can just add the last 2 elements of the right array without worrying about comparisons. Hope that helps :)

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

    Great video bro... keep it up👌🤙

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

    can u make count inversion video pls .........this is very nice explanation

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

    Great Video please make data structure and algorithms full series playlist

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

    such a nice explanation thankyou

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

    in minute 6 you said seven ? its thats means 5 ? yes or no?

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

    so this is why you Germans are so good at engineering. such a thorough explanation!

  • @hicham_baydoun
    @hicham_baydoun Рік тому +1

    in this code there is an issue where u should put a base case before the while loop
    if len(l1)

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

    awesome explanation

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

    thanks for this great explanation

  • @al-washisarkerturan8913
    @al-washisarkerturan8913 6 місяців тому

    Is O(n) the time complexity of this code or is it O(nlogn)?

  • @darcash1738
    @darcash1738 10 місяців тому

    Awesome explanation, but based on how you implement it, the left side should be smaller in the example you give: Len = 7, 7//2 = 3. 0:3 captures the actual range of 0 thru 2, or the first 3 elements

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

    Very easy to understand thx!

  • @pokerchannel6991
    @pokerchannel6991 3 місяці тому

    kind sir: how come the merge_sort(left and right arr) don't take the result of the sort? That is so cool and a bit mind bending. I need to look closer to really see how your code words. It is like the recursion layers didn't matter. I don't even think it is because the arr main list is referencing the same address, as the reason. It is voodoo code, that is very fascinating! I gotta look closer at it. I hope you read this comment.. thx.

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

    Wow man ur underrated!!

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

    Great video keep it up

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

    Thanks for sharing!

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

    Y u don't show the results in screen

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

    I have a question about your method. Is this fully recursive? Seems like only the first part.

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

    how is array of size one already sorted, I can't understand that part

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

      well how can it be unsorted, for example a list of one: [25] cant be out of order because theres only one element

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

    Awesome tutorial

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

    Hi Team, in the line 2, it is giving me runtime error, stating mximum depth recursion comparison