Why Is Merge Sort O(n * log(n))? The Really Really Long Answer.

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 18. 02. 2019
  • Free 5-Day Mini-Course: backtobackswe.com
    Try Our Full Platform: backtobackswe.com/pricing
    đŸ“č Intuitive Video Explanations
    🏃 Run Code As You Learn
    đŸ’Ÿ Save Progress
    ❓New Unseen Questions
    🔎 Get All Solutions
    Question: Analyze the total work that Merge Sort performs as an exact function of n, the length of the input list.
    My Old MergeSort Video: ‱ Merge Sort - A Step By...
    The Infinite Series 1 + 2 + 4 + 8 + ... : en.wikipedia.org/wiki/1_%2B_2...
    Logarithm Rules: www.chilimath.com/lessons/adv...
    ++++++++++++++++++++++++++++++++++++++++++++++++++
    HackerRank: / @hackerrankofficial
    Tuschar Roy: / tusharroy2525
    GeeksForGeeks: / @geeksforgeeksvideos
    Jarvis Johnson: / vsympathyv
    Success In Tech: / @successintech
  • Věda a technologie

Komentáƙe • 415

  • @BackToBackSWE
    @BackToBackSWE  Pƙed 5 lety +44

    Table of Contents (If you watch this all, you will know why Merge Sort is upper bounded by O(n * log(n)))
    What This Video Will Cover 0:00 - 0:40
    The Fundamental Subroutines 0:40 - 1:32
    The Split Subroutine 1:32 - 8:35
    The Split Subroutine In Code 8:35 - 11:05
    [ Old Clip ] Merge Sort Execution Trace 11:05 - 14:38
    The Merge Subroutine 14:38 - 16:54
    The Recurrence Relation 16:54 - 21:45
    Investigation: What Work Is Done Per Level? 21:45 - 27:10
    Getting Exact: Solving For Total Work Across All Levels 27:10 - 30:50
    My Whole Life Has Been A Lie 30:50 - 34:51
    Checking Our Work 34:51 - 35:48
    Wrap Up 35:48 - 36:30
    The code for Merge Sort is in the description, fully commented for understanding.

    • @fpv_am
      @fpv_am Pƙed 5 lety +9

      I've really applause you when I understand something from your explanation)

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 4 lety +1

      ye

    • @mostafaomar5309
      @mostafaomar5309 Pƙed 4 lety +15

      @@BackToBackSWE Hi, you're doing a really good job, thank you, but I couldn't find any code in the description? :)

    • @hil449
      @hil449 Pƙed 2 lety

      you really should think about becoming a university professor if you enjoy teaching man. Your teaching methods are superb

  • @34521ful
    @34521ful Pƙed 5 lety +228

    The patience you had to go through every literal step, god damn, much appreciated (once again) man.

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 5 lety +42

      This video is way too long and discourages clicks from passive users but whatever. It needs to be in detail since brevity would confuse and glaze over critical building blocks to the larger understanding.

    • @ramondelarosa4895
      @ramondelarosa4895 Pƙed 4 lety +4

      @@BackToBackSWE You really are a true MVP! Your videos on time complexity algorithms are a godsend! Cheers again!
      Keep up the good work!!

  • @Ghareonn
    @Ghareonn Pƙed 4 lety +118

    Bro, you should be the one teaching me algorithms instead of my college professor, good job!

  • @paukatable
    @paukatable Pƙed 4 lety +31

    How COME a person can be this talented at teaching? Thank you a million times!

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 4 lety +6

      im just so average that I had to learn harder than other lol

  • @tomevans2417
    @tomevans2417 Pƙed 3 lety +36

    In my 4 years of doing a Computer Science degree I never had a lecturer explain it this well

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 3 lety +4

      ye

    • @minus-infinity
      @minus-infinity Pƙed rokem

      thats why our youtuber bros are here 😜😜 they must have gone through same problems

  • @Rhunyc
    @Rhunyc Pƙed 4 lety +35

    Hey man, just wanted to add a +1 to the supportive comments. I absolutely love how you do not jump over steps assuming people know what's happening. I have a brain that needs to know why every step is included even if some might find it common sense, so this is wonderful for me. I subscribed to you because I think you deserve it. :) Have a wonderful day!

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 4 lety

      Nice, welcome to the đŸ’©show

    • @supastar25
      @supastar25 Pƙed 4 lety

      He explains it so clearly on every step..subscribed

  • @Egrodo1
    @Egrodo1 Pƙed 5 lety +47

    Also found your videos through Leetcode. As someone without a traditional CS background, you have a talent for explaining things in a very clear way. I imagine making these videos must be exhausting, but I do hope you continue. Even if that means one per month :)

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 5 lety +10

      Thanks and nah...we are going all the way this year.

  • @cristian-razvanpopa7101
    @cristian-razvanpopa7101 Pƙed 2 lety +6

    For those getting stuck at [29:09], there is a 2^i before the parenthesis, but it might not be visible due to board reflections. Thank you for the video as it helped a lot to understand how its complexity is calculated for the first time.

  • @thestudycoven8771
    @thestudycoven8771 Pƙed 3 lety +13

    ^ THIS is how to teach
    The way most professors teach is as if they are giving a refresher of something the students have already learned rigorously. They should watch this video and take notes on how to teach.

  • @domyi6953
    @domyi6953 Pƙed 5 lety +2

    I found this channel through your comment in LeetCode, and I'm so glad I was able to find your channel before my tech interview. THANK YOU SO MUCH. KEEP IT GOING

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 5 lety +7

      I'm getting tired to be honest. So much effort, 4-6 hours a day. But, I'll keep going.

  • @akshitarora470
    @akshitarora470 Pƙed 4 lety +1

    This is the best analysis out there, you leave me in awe with your explanations time and again. Really waiting for new videos! This was the only one I had left by mistake, sad that there's no fresh content coming from you on youtube. Anyways, thanks a ton for all that you are providing to the community!

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 4 lety

      great and thanks. and I'm spending most of my time building backtobackswe.com code-wise and building our content team. and sure - that's my goal

  • @anthonylee1309
    @anthonylee1309 Pƙed 4 lety +6

    Coming from a non-computer science major, real understanding sorting part was always a real pain. (Even with my experience in working as a sw developer).
    Just after watching this video for about 5 mins, lightening struck my head.
    I've paid more than a few hundred bucks on algorithm + data structure courses, and was never satisfied once.
    Your explanation, though it is free, is by far the best, and the only one I was completely satisfied with.
    No, it's not just satisfaction, but the best I have ever encountered in my life.
    Thank you once again. Truly appreciate your great work.

  • @hegyilevente221
    @hegyilevente221 Pƙed 4 lety +1

    You are a legend in teaching. Pure and simple explanation with a very patient attitude. It's clear that you don't do the videos to be done, you really want to transfer your knowledge, and you do it the best. I'm happy that i find you. Keep up the good work brother!

  • @dragoslavmilutinovic5731
    @dragoslavmilutinovic5731 Pƙed 4 lety +8

    You are literally only youtuber who gives deep intuition behind this concept, you are giving us feeling like we could invent this by own, much respecttttt

  • @nullbrain8552
    @nullbrain8552 Pƙed 4 lety +1

    I find this to be incredibly valuable during a term where the only contact I get with my teachers is through zoom. I managed to get through my first computer science classes online with little to no issues but now that I am taking data structures I am finding that my assignments are taking 15+ hours and I think this is because of not getting to have those one on ones with my teacher. The way you have put together these videos has answered many questions that I didn't even know that I had. Thank you!!!

  • @adrijasamanta7949
    @adrijasamanta7949 Pƙed 3 lety +8

    You have just converted a process of time complexity of O(n ^ 1000000) to O(log n) in this video with your awesome teaching talent. Really like the way you teach with immense patience and dedication.

  • @user-rq9kd7xu5x
    @user-rq9kd7xu5x Pƙed 3 lety +1

    you are amazing!! I'm planning to watch all your videos about sorts!!! Thank you so much for making this video. I can see the amount of effort that was put in to it!!!

  • @vanducnguyen346
    @vanducnguyen346 Pƙed 4 lety +3

    You need way more recognition man As a person who do programming, i always curios about the concrete math model behind what is commonly told I literally watch your video to find out how exactly the time complexity got deduced And man it was a damn good video Keep it up man We do need more curiosity people like you

  • @amypellegrini1732
    @amypellegrini1732 Pƙed 3 lety

    I've seen (and read) lots of explanations with complexity analysis of merge sort, but this is the one that clicked for me! Thanks for this wonderful explanation.

  • @charlieminaya3534
    @charlieminaya3534 Pƙed 3 lety

    The. Best. Explanation. Ever. I have months confused about what it actually meant for it to be nlogn, and it all makes so so much sense now. Thank you a million times. Your videos are THE best. Much appreciated.

  • @phenipatel7089
    @phenipatel7089 Pƙed 5 lety +3

    Thank you so much, I've never understood the analysis of Sorting Algorithms correctly but you gave a remarkable explanation on all my doubts on it.

  • @antongrau7963
    @antongrau7963 Pƙed 3 lety

    Thanks for you explanations. I am a student from germany. The content was also described in the script of my studies but your way to fill up this content with energy and a impressive way of didactics let me understand the topic. You have a sensitiv way to teach.

  • @karthikrockrr
    @karthikrockrr Pƙed 4 lety +2

    This material is gold and it gives you an order of solving leetcode problems to understand merge sort in dept. Kudos to the creator !

  • @BornYooper
    @BornYooper Pƙed 5 lety +1

    You have a great talent for explaining these concepts. I wish this content would have been available last year when I was taking my algorithms class!

  • @starbloods0013
    @starbloods0013 Pƙed 4 lety +6

    literally the best explanation i've ever gotten. i finally get it. i go to a top university but i regret wasting my money when your videos teach me better than my professors

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 4 lety +4

      nah, a college education is valuable srs. Program and build in ur own time. School itself sucks sucks sucks, but the output you get molds your mind into a critical thinking machine sorta...hard to explain.

    • @starbloods0013
      @starbloods0013 Pƙed 4 lety

      @@BackToBackSWE gotchu. i'll take your word for it. thanks for the help and i hope ya'll keep making videos

  • @LenCedeno
    @LenCedeno Pƙed 2 lety +1

    First time I ever saw ANYBODY even attempt to explain how we arrive at log n or n log n or all these math conclusions. And I mean even in texts supposedly written to help you understand algorithms. Most times the lecturers/writers just rush to the final result. Way to go brother!!

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 2 lety

      Thanks buddy! do try the 5 day free mini course or subscribe to our courses with 30% discount for some exclusive content b2bswe.co/3HhvIlV

  • @ellenahpage8914
    @ellenahpage8914 Pƙed 3 lety +1

    I love how you broke this down to the fundamentals. So helpful, I smashed the subscribe button so hard my clicker nearly exploded

  • @sapokee2830
    @sapokee2830 Pƙed rokem

    This man is an absolute gold mine. Thank you so much, this is the clearest explanation of _anything_ I have ever seen.

    • @BackToBackSWE
      @BackToBackSWE  Pƙed rokem

      Thank you, appreciate it 😄 Also check out our Free 5 Day DSA Interview Prep Mini-Course - backtobackswe.com/ 🎉

  • @Owen7768
    @Owen7768 Pƙed 2 lety

    After a couple of days and a lot of videos, finally I found a video that really help to understand the time complexity of merge sort. thank you !

  • @yarashamali8061
    @yarashamali8061 Pƙed 3 lety +1

    i mean WOW ! i was honestly struggling with my algorithm class this semester especially since its on zoom and the professor is all over the place and nothing made sense !!!!! but in just 36 minutes i understood what my professor tried to teach in 4 hours !!!!! so thank you so much ure a life saver ! would love to donate

  • @yannaleyva4554
    @yannaleyva4554 Pƙed 5 lety +2

    I think I love you.... Or at least your explanations and fantastic teaching skills xD Please keep up the fantastic work. Very high-quality and helpful, unlike most resources I have looked at previously! I also appreciate you including the table of comments with the times. Subscribed :)

  • @coolio_catio
    @coolio_catio Pƙed 3 lety

    This was so so helpful, thank you so much for putting in all the work required to make this video

  • @scodz2007
    @scodz2007 Pƙed 4 lety +1

    Terrific work, hats off to your way of explanation. I follow tons of youtuber. Nobody isn't close to your explanation approach or skill. Keep up the awesome work!

  • @WillASalazar
    @WillASalazar Pƙed 2 lety

    This is literally by far the best explanation I have ever seen. Good going!

  • @theappgirl6615
    @theappgirl6615 Pƙed 4 lety +1

    This is sooo informative. I finally understood Merge Sort and most importantly, why it has O(NlogN) time complexity. thank you so much ! :)

  • @heathledger7291
    @heathledger7291 Pƙed 4 lety +1

    this is the stuff i was trying to find about mergesort, not just the how, but also the why,
    everyone else seems to think teaching splitting and merging functions is enough, but until u realize how the process is unfolding practically u never truely "realize" the essense of mergesort.
    The thing with recursive algorithms is that they seem very easy and obvious ,until u start thinking how the computer actually handles all the states,calls,order of calls,variables etc,
    thats probably why everyone tries to talk about recursion on a superficial level.
    This video is so great btw .Very in-depth.

  • @ekejma
    @ekejma Pƙed 3 lety

    Doing the math here was one of the high points of my review of computer science. One of the best videos because of the deeper understanding that is so effectively presented.

  • @KuldeepYadav-jw7jn
    @KuldeepYadav-jw7jn Pƙed 4 lety +1

    No other video on the internet can describe this any better, huge respect for you

  • @tejshenanigans8070
    @tejshenanigans8070 Pƙed 3 lety +1

    why aren't your videos not mostly watched ?
    This explanation is strongly etched into my mind .Even if forget in future how did it get that time complexity ,you gave me confidence to derive the whole process .Thank you

  • @user-ml9lh7zn7d
    @user-ml9lh7zn7d Pƙed 3 lety +1

    I thought I'd skip it once I feel lost, but it never happened, great job. Wish u more subscribers

  • @bulioh
    @bulioh Pƙed 2 lety

    You have taken the amount of time for me to understand merge sort from O(n!) to O(n*log(n)), thank you

  • @dawn_connor
    @dawn_connor Pƙed 2 lety

    i'm graduated from uni, but my data structures and algorithms professor sucked. this section of CS has been the bane of my existence for a long time now, and i've really appreciated this video. thanks so much. i needed a long explanation like this.

  • @suyuan9158
    @suyuan9158 Pƙed 4 lety +1

    So clear, bro! Wonderful job, cannot wait to subscribe!

  • @vrashankraom
    @vrashankraom Pƙed 4 lety +3

    Awesome bro! I have never seen such a good explanation ever!

  • @nolanhellen4888
    @nolanhellen4888 Pƙed 2 lety

    Holy Molly, you rocks, this is the best explanation for a student, which I can find on the internet!

  • @hrithikjaysingpure9623
    @hrithikjaysingpure9623 Pƙed 3 lety +1

    This is the best explanation of the merge sort on youtube.

  • @harshraj3255
    @harshraj3255 Pƙed 3 lety

    Thank you so much for your effort! Great explanation. Godspeed to you!

  • @ryancodrai487
    @ryancodrai487 Pƙed 3 lety

    I thought your explanation of the big o complexity was superb. I really liked you worked through the proof and then did the mathematical proof and showed that using the formula got the right answer. Definitely pushed my understanding further there.

  • @PenStab
    @PenStab Pƙed 2 lety

    My god, this is fantastic. I have ADHD but you present this in such a thorough and interesting way, it's not problem paying attention. Thanks so much!

  • @Ian-bb7vv
    @Ian-bb7vv Pƙed 4 měsĂ­ci

    thank you for this. what you are doing is really helping people. i think you know that, but still want to help confirm your work. thank you

  • @mia-so4ry
    @mia-so4ry Pƙed 2 lety

    this was amazing, thank you so much. This video made me fall even deeply in love with CS and math!!

  • @langwen8685
    @langwen8685 Pƙed 4 lety +1

    Really appreciate your patience and clear presentation. I understand why it's O(nlog n) after watching this video. In addition, I really love your clear pronunciation! Really really appreciate cus you do better than my college professor!

  • @zettbk
    @zettbk Pƙed 4 lety +1

    Just wow! This is a brilliant piece of material!

  • @tomaustin9166
    @tomaustin9166 Pƙed 5 měsĂ­ci

    A true masterpiece of a video and a true masterpiece of a man. You are such a good teacher and I'm glad I can learn my University course of you! :)

  • @willianfn
    @willianfn Pƙed rokem

    Thank you, it was so didactic. I loved your explanation and could really understand what was going on.

  • @ARATHI2000
    @ARATHI2000 Pƙed rokem

    Great job. Appreciate the Big-O complexity details!

  • @janewu4660
    @janewu4660 Pƙed 2 lety

    Thank you so much for your detailed explanation!! It really helps a lot!!!!

  • @danrussell1057
    @danrussell1057 Pƙed 3 lety

    You've taken something that twisted my head to epiphany. Thank you so much!

  • @nniazoff
    @nniazoff Pƙed 4 lety +3

    Very clear and detailed. Great job.

  • @dishagupta7446
    @dishagupta7446 Pƙed 3 lety +1

    I always had tough time understanding time complexities but now it's all clear after watching your videos❀

  • @MubashirAR
    @MubashirAR Pƙed 4 lety +1

    This channel is a goldmine 👌
    Superb explanation

  • @skhan3530
    @skhan3530 Pƙed 3 lety

    Thanks a lot,it was great .Really helped me in understanding the concept. Appreciated! :)

  • @frankgarcia6673
    @frankgarcia6673 Pƙed 3 lety +2

    If I could like this video 10000 times, I would.. Thank you so much for the clear explanation !

  • @jigarsingh600
    @jigarsingh600 Pƙed 4 lety +1

    This forest explanation is the best recursion call stack explanation of an algorithm on internet!

  • @danielspatz7460
    @danielspatz7460 Pƙed 4 lety +1

    Fantastic video! Thank you for the content!

  • @GabrielPerez-ml2go
    @GabrielPerez-ml2go Pƙed 3 lety +3

    bro, yore easily better than all my college teachers at explaining this, hell yeah im subscribing

  • @alex-gz7ud
    @alex-gz7ud Pƙed 3 lety

    YOU ARE AMAZING!!!!! Thank you for making this video!!!!

  • @alan0428a
    @alan0428a Pƙed 3 lety

    I finally know why the time complexity of merge sort is nlog(n) now.....Thanks a lot!

  • @AhmedMostafa-so2jp
    @AhmedMostafa-so2jp Pƙed 3 lety

    this is the best explaination ever to anything!!

  • @saikiran-xh5lt
    @saikiran-xh5lt Pƙed 3 lety

    I really appreciate your efforts, thanks, bro.

  • @Onelegtrev
    @Onelegtrev Pƙed 3 lety +1

    I feel that my brain has expanded 10 fold by watching your video. Thank you!

  • @harshitrastogi9078
    @harshitrastogi9078 Pƙed 4 lety +1

    Thank you for this! Great explanation!

  • @DivinityinLove
    @DivinityinLove Pƙed rokem

    Great explanation. I got lost towards the last 6 minutes once the distribution started and couldn't follow anything after we arrived at n log n. :/ But will return to watch again :) great break down. Thanks.

  • @youraverageprogrammer7532
    @youraverageprogrammer7532 Pƙed 4 lety +1

    Amazing video, just earned a new subscriber thank you!

  • @adamgordon862
    @adamgordon862 Pƙed 5 lety

    Awesome video! Definitely cleared things up for me.

  • @sameerarasanga8975
    @sameerarasanga8975 Pƙed 2 lety

    To the point well explained, couldn,,,'t imagine how 36 min goes so quickly

  • @adamodimattia
    @adamodimattia Pƙed 5 lety +1

    Great man, great explanation, thank you.

  • @yuvrajbansal7979
    @yuvrajbansal7979 Pƙed rokem

    Thank u so much, this video helped me a lot!

  • @zelda8697
    @zelda8697 Pƙed rokem

    i would say you still need to watch few other videos to understand the concept of merge sort and the code as well, however he did great job explaining some concepts in this video, its amazingly helpful

  • @serogozin
    @serogozin Pƙed 2 lety

    Thank you for your videos, they helped me a lot for my DSA exam

  • @k.herzog2365
    @k.herzog2365 Pƙed 4 lety +1

    Excellent explanation, thank you so much!

  • @arvindojha1101
    @arvindojha1101 Pƙed 4 lety +1

    Good job man ! , very neat and nice explanation.

  • @SyedObaidullah555
    @SyedObaidullah555 Pƙed 4 lety +1

    Amazing video mate! Thanks a ton !

  • @shishirpai380
    @shishirpai380 Pƙed rokem

    Everything makes perfect sense now! Thank you

  • @JujutsuMan
    @JujutsuMan Pƙed rokem

    I got to say that you are super AMAZING!!!
    My professor just skipped the total process....
    like T(8)=T(n/2)+T(n/2)+(n-1) then bombed Worst case O(nlogn)
    You completely fill out all my gap about why that is O(nlogn)

  • @tsilikitrikis
    @tsilikitrikis Pƙed 3 lety

    Hi guys, took me some time to understand at the end how the nlogn was extracted by the ÎŁn.
    its very simple, because 'i' is not there in the sum(ÎŁn) you Just adding n, logn times(logn=times you split the problem or levels). So here the serie has 3 terms but he start to count from 0 to logn - 1, so he has logn sums of n, or
    n + n +n =3*n=log(8)*n
    =n*logn
    im new to CS and try to Learn algorithms, great video man thank you very much!!

  • @hiteshnagothu887
    @hiteshnagothu887 Pƙed 4 lety +5

    Fantastic job !!
    Have we ignored the time or amount of work required to split each array until the base array arrives?? Let's say if a million items are given as input, does the amount of time to split until base has any significant effect on total running time asymptotically?

  • @induction7895
    @induction7895 Pƙed 3 lety

    Beautifully explained.

  • @vishnupriya47659
    @vishnupriya47659 Pƙed 3 měsĂ­ci

    That's the best explanation for any beginner to understand.

  • @tirthjayswal9895
    @tirthjayswal9895 Pƙed 4 lety +1

    Thank You So much I was Finding exactly this

  • @Youssiflagrosse
    @Youssiflagrosse Pƙed 3 lety

    I always wanted to know where this came from. Awesome way to teach. Great job.

  • @rishabh55
    @rishabh55 Pƙed 5 lety +9

    So much effort in a video for merge sort

  • @truthseeker8767
    @truthseeker8767 Pƙed 4 lety +5

    No one:
    Lecturers(not everyone):
    Me: This is how to put your effort in exemplifying down to the basics for the sake of learning!

  • @doudouhao1678
    @doudouhao1678 Pƙed 2 lety

    You're my life saver!! Thank you!

    • @BackToBackSWE
      @BackToBackSWE  Pƙed 2 lety +1

      Thank you, glad I could help 😀
      Do check out backtobackswe.com/platform/content
      and please recommend us to your family and friends 😀

  • @richardwang3913
    @richardwang3913 Pƙed 5 lety +1

    love you man. so great explanation

  • @irrelevanceuk
    @irrelevanceuk Pƙed 3 lety

    Very revealing thank you. đŸ‘đŸŸ

  • @josephcohen734
    @josephcohen734 Pƙed 3 lety

    Each sentence was a little incoherent individually, but as a whole this was a pretty clear explanation. Definitely cleared it up for me.

  • @MauricioVelasco93
    @MauricioVelasco93 Pƙed rokem

    Wooo amazing!!
    Thanks!

  • @anmjubaer
    @anmjubaer Pƙed 3 lety +2

    Superb video. A huge effort. Thanks.
    Btw, can you do Master theorem? I find it a bit confusing.

  • @theManTheGuy_inTheMiddle

    I'm very thankful past this video. Thank u. Hello from Brasil!!

    • @BackToBackSWE
      @BackToBackSWE  Pƙed rokem

      Glad it helped😄 Also check out our FREE DSA Interview Prep Mini-Course - backtobackswe.com/ 🎉