Merge Sort In Python Explained (With Example And Code)

Sdílet
Vložit
  • čas přidán 19. 06. 2020
  • Merge Sort is an efficient sorting algorithm with O(nlogn) running time. In this video I show you a quick example and how to implement this algotrithm in Python step by step.
    This video is part of the basic algorithms in Python playlist. The goal is to get an understanding of basic computer science algorithms and their implementation in Python.
    For more coding videos subscribe to my youtube channel.
  • Věda a technologie

Komentáře • 177

  • @Eduardo_C
    @Eduardo_C Před 2 lety +45

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

  • @EagerEggplant
    @EagerEggplant Před 2 lety +4

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

  • @RoMAndrix
    @RoMAndrix Před 2 lety +6

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

  • @user-oz4ts5mv5h
    @user-oz4ts5mv5h Před 2 lety +8

    The most clearly explained one. Great job! Thanks!

  • @anall3l3
    @anall3l3 Před 3 měsíci +1

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

  • @shivenmehru1571
    @shivenmehru1571 Před 2 lety +2

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

  • @Kamil-rf5qn
    @Kamil-rf5qn Před 5 měsíci

    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!

  • @moussadiakhate9022
    @moussadiakhate9022 Před 2 lety

    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

  • @ozzy4738
    @ozzy4738 Před rokem +1

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

  • @yiqiongxiao5255
    @yiqiongxiao5255 Před 2 lety

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

  • @GermansLikeBeer
    @GermansLikeBeer Před 2 lety +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

  • @RootUser-lg1qh
    @RootUser-lg1qh Před rokem

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

  • @aj_hari_7
    @aj_hari_7 Před 5 měsíci

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

  • @waaaaaaaayyyyyynnnee
    @waaaaaaaayyyyyynnnee Před 3 lety +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

  • @DanielSmith-uj7rr
    @DanielSmith-uj7rr Před 2 lety +1

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

  • @jaganchowhaan9648
    @jaganchowhaan9648 Před 2 lety

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

  • @Strydomlisa
    @Strydomlisa Před 2 lety

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

  • @sucraloss
    @sucraloss Před rokem +33

    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.

  • @Eddie4k
    @Eddie4k Před rokem

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

  • @shwetarajapure8830
    @shwetarajapure8830 Před rokem

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

  • @t-distributedkid3825
    @t-distributedkid3825 Před rokem +1

    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 Před 5 měsíci

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

  • @rhodabaruch4
    @rhodabaruch4 Před 8 měsíci

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

  • @tusharnaagar2587
    @tusharnaagar2587 Před 6 měsíci

    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

  • @SanjuKumar-hk8yy
    @SanjuKumar-hk8yy Před 3 lety +2

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

  • @arnavverma2135
    @arnavverma2135 Před 3 lety +24

    This was amazing, keep it up...

  • @olafschlammbeutel
    @olafschlammbeutel Před 4 lety +6

    Exactly what I need right now

  • @ligrt2426
    @ligrt2426 Před 3 lety

    great video with details and simplicity, thank you!

  • @user-qs1gr7tl6s
    @user-qs1gr7tl6s Před 7 měsíci +2

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

  • @alexrubio9507
    @alexrubio9507 Před 5 měsíci

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

  • @AnsariAnime
    @AnsariAnime Před rokem

    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😊😊

  • @son0funiverse
    @son0funiverse Před 3 lety +1

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

  • @mohammedhrima1654
    @mohammedhrima1654 Před rokem

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

  • @kieseatic9983
    @kieseatic9983 Před 5 měsíci

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

  • @julian7934
    @julian7934 Před rokem +6

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

  • @ahmet8266
    @ahmet8266 Před 2 lety

    thank you, I finally understood the merging step.

  • @mythbuster4315
    @mythbuster4315 Před 7 měsíci +1

    excellent tutorial! right before my exam!

  • @santiagolicea3814
    @santiagolicea3814 Před rokem

    You explain things so well, thank you!!!

  • @maxfrischdev
    @maxfrischdev Před 8 měsíci +1

    Great explantion! Toll gemacht! 🙂 Danke Felix

  • @TheKorkahn
    @TheKorkahn Před 4 lety +4

    I love sorting numbers! Thank you for the great tutorial

  • @Nick-gs4em
    @Nick-gs4em Před 2 lety

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

  • @Desh_Bhakt_
    @Desh_Bhakt_ Před 2 lety

    Sirrrr wonderfully explained.
    just loved it.

  • @samuell77799
    @samuell77799 Před 2 lety +1

    Thanks, the explanation helped me a lot.

  • @LukeAvedon
    @LukeAvedon Před rokem

    The best video of all the videos!

  • @kostekukowski4383
    @kostekukowski4383 Před 2 lety +1

    that was awsome, you've helped me so much

  • @ManuelGozzi
    @ManuelGozzi Před 25 dny

    Very well explained. My algorithms class was terrible lol.

  • @lajoskicsi6910
    @lajoskicsi6910 Před rokem

    Thank you, this was a super useful video!

  • @ayushshubhankar858
    @ayushshubhankar858 Před 5 měsíci

    Thanks for these videos felix!!

  • @luvlifk
    @luvlifk Před 4 měsíci

    very nicely explained merge sort.

  • @ej6552
    @ej6552 Před 2 lety

    So informative! Big up from jp🇯🇵

  • @vm7240
    @vm7240 Před 2 lety

    thank you! very easy to understand explanation

  • @ZahZah571
    @ZahZah571 Před 2 lety

    Very clear! Thank you very much

  • @NHCSKIRAN
    @NHCSKIRAN Před 2 lety

    Was very helpful in the last minute

  • @tljstewart
    @tljstewart Před 2 lety

    Simply Beautiful, thank you.

  • @rogueceska
    @rogueceska Před rokem

    best explanation yet thanks.

  • @bodalajahnavi6272
    @bodalajahnavi6272 Před 2 lety +1

    Very Easy to learn. Thanks

  • @IslamSoliman_Sci
    @IslamSoliman_Sci Před 2 lety

    I Loved It So Much.. Good Luck

  • @lena-ck9wz
    @lena-ck9wz Před 4 lety +4

    i really like your videos

  • @miyurux
    @miyurux Před 2 lety

    Great video bro... keep it up👌🤙

  • @Code4You1
    @Code4You1 Před 2 lety

    Very easy to understand thx!

  • @pranjalsrivastava4265
    @pranjalsrivastava4265 Před 2 lety

    brilliant explanation, Hats off

  • @shaiksalman9010
    @shaiksalman9010 Před 2 lety

    love ur lucid explanation❤❤❤❤❤

  • @vihangatharushan4477
    @vihangatharushan4477 Před 4 měsíci

    actually this is very simple and wow explanation

  • @logofios
    @logofios Před rokem

    Cool realization!

  • @charliewebb7244
    @charliewebb7244 Před měsícem +1

    perfect video. 10/10

  • @lookaway8496
    @lookaway8496 Před rokem

    thanks for this great explanation

  • @cachaceirosdohawai3070

    Great video keep it up

  • @amanaggarwal4061
    @amanaggarwal4061 Před 2 lety

    Great Video please make data structure and algorithms full series playlist

  • @bulisharma1138
    @bulisharma1138 Před 2 lety

    such a nice explanation thankyou

  • @redeemuser
    @redeemuser Před rokem

    Neat clean and concise

  • @zakariaryanmechkak3761

    Much better than the million videos out there just talking rubbish

  • @venkatabora1459
    @venkatabora1459 Před 2 lety +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 Před 2 lety

      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.

  • @riyazshaik864
    @riyazshaik864 Před 7 měsíci

    awesome explanation

  • @girish6064
    @girish6064 Před rokem

    Thanks for sharing!

  • @harvzgames9622
    @harvzgames9622 Před rokem

    How would I do with this names and scores, like a highscore table where it will put highest score at the top but if 2 players have the same score it goes alphabetical

  • @latest_news_stories
    @latest_news_stories Před rokem

    Awesome tutorial

  • @sudhakartripathi3879
    @sudhakartripathi3879 Před 3 lety +1

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

  • @abufaya4155
    @abufaya4155 Před 2 lety

    Wow man ur underrated!!

  • @hicham_baydoun
    @hicham_baydoun Před 7 měsíci +1

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

  • @mohsinkhattak2840
    @mohsinkhattak2840 Před 2 lety +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 Před rokem +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 :)

  • @python_knooobb
    @python_knooobb Před 9 měsíci

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

  • @baochu8082
    @baochu8082 Před rokem

    Hi, can someone please explain to me the recursion part? I can only vaguely understand how recursion works in this problem. Thank you

  • @cateva20
    @cateva20 Před 2 lety

    thank you very much!!!!

  • @kunaldev2590
    @kunaldev2590 Před 2 lety

    what if we have 2 arrays and we have to sort them by mergesort?

  • @barret3534
    @barret3534 Před 6 měsíci

    Thaks Felix.

  • @sanooosai
    @sanooosai Před 3 měsíci +2

    thank you

  • @osyman782
    @osyman782 Před 2 lety +2

    Why does the function have no return

  • @dikshyantauprety4020
    @dikshyantauprety4020 Před 2 lety

    Thank you so much

  • @umeboshi_
    @umeboshi_ Před měsícem +1

    Thank you

  • @NavneetKumar-fr9wj
    @NavneetKumar-fr9wj Před rokem

    Keep making tuts :)

  • @mahadihasan7300
    @mahadihasan7300 Před 2 lety

    Thanks buddy 😍

  • @egemen_ozturk
    @egemen_ozturk Před 2 lety

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

  • @qaipak1
    @qaipak1 Před 3 lety

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

  • @egipebriyawan1455
    @egipebriyawan1455 Před 3 lety +1

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

  • @al-washisarkerturan8913
    @al-washisarkerturan8913 Před měsícem

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

  • @darcash1738
    @darcash1738 Před 4 měsíci

    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

  • @abhijithsubash6160
    @abhijithsubash6160 Před 2 lety

    Thank You Sir

  • @marwasolh9449
    @marwasolh9449 Před rokem

    big thankss

  • @lhb2108
    @lhb2108 Před 3 lety

    dont we need base case?

  • @user-oz6fr9xr1h
    @user-oz6fr9xr1h Před 5 měsíci

    shouldn't the first while loop conditional read "if left_arr[i]

  • @yamanarslanca4337
    @yamanarslanca4337 Před rokem +3

    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 Před rokem

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

    • @MahlakaSami
      @MahlakaSami Před rokem

      @@gabrielvictorrusso5931 did you try it?

    • @gabrielvictorrusso5931
      @gabrielvictorrusso5931 Před rokem

      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 Před rokem

      @@MahlakaSami Same issues.

    • @paulster185
      @paulster185 Před 11 měsíci +1

      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 :)