Merge Sort - How it works ? Algorithm + Code

Поділитися
Вставка
  • Опубліковано 16 лип 2024
  • In this tutorial, Prateek Bhayia takes through an interesting sorting algorithm Merge Sort, which sorts an array in O(nLogn) Time.
    Code : codingblocks.com/ide/#/s/3832
    Like our FaceBook Page - / codingblocksindia
    Follow us on Instagram - / codingblocks
    Follow us on Twitter - / codingblocksin
    Source code available on -github.com/coding-blocks
    For more interesting tutorials - / @codingblocksindia

КОМЕНТАРІ • 135

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

    Get the best Offers & Deals during the Big Diwali Sale: cb.lk/diwaliyt

  • @RaviYadav-xg2hy
    @RaviYadav-xg2hy 4 роки тому +24

    Just awesome !! I've been watching videos and studying articles on Merge sort .......feeling relaxed to say that this video was last in understanding conceptually and crystal clearly !!! Thanks a lot mahn !!

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

      You can check out complete online course on Coding blocks online.

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

      @@CodingBlocksIndia Is it free or paid

  • @NoobieX676
    @NoobieX676 5 років тому +6

    One of the best video on merge sort I have ever seen!! Simply amazing! I was tensed as I could not understand the merge sort but you saved me! You are a lifesaver may God bless you!

  • @tusharbarman1924
    @tusharbarman1924 5 років тому +9

    This is by far the best code I have seen. Clean and simple.

  • @yashdeokate1210
    @yashdeokate1210 4 роки тому +15

    100's of attempt to learn this thing and who knew that a UA-cam video will make it clear.. thank you sir

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

      @@dhwajsharma oh really..are you replying to me or " your need of self validation which you are trying to satisfy by imagining someone below you" .🤗anyway i hope you get a life🙏🏿

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

      And i love how you liked ur own comment

  • @MultiSilko
    @MultiSilko 5 років тому +9

    This is the best explaination of this sorting method i've seen so far!!
    Thank you and ofcourse keep up the good work.

  • @Jeetu0301
    @Jeetu0301 6 років тому +10

    Easiest approach.
    Understood in just one go.
    Thanks.

  • @surya-or9mc
    @surya-or9mc 5 років тому +3

    Your approach is very nice sir instead of explaining a block of code you just explained by the concept and then written the code.really loved it.eagerly waiting for your upcoming videos.

  • @junaidali9603
    @junaidali9603 5 років тому +6

    Excellent video I have been wandering for a long time to understand this merge array concept but failed all the time.
    This video at least gave me the confidence that with 2 or 3 times I will be able to understand this concept at 100 percent.
    Thank You

    • @CodingBlocksIndia
      @CodingBlocksIndia  5 років тому

      Thanks Junaid Ali for the compliment 💯. Please subscribe us for more such videos ✌️

  • @Jacobwlane
    @Jacobwlane 4 роки тому +1

    You absolute legend - I was watching a course where merge sort came up and the implementation they used deliberately only allowed it to function with a number of variables that were a factorial of 2 and it was absolutely bending my mind but this explained it beautifully and I FINALLY understand it 😂 If you have a tip jar for donations I'd love to drop you one - the least I can do is give you guys some coffee money!!

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

    Why we are taking tempryory array can we assign the values into the same array ? And save the overwritten element before assigning the small value.

  • @rohantumma1450
    @rohantumma1450 5 років тому +1

    very simple explanation and simplest way to implement mearge sort.
    appreciate it.
    keep making videos like this.
    thanks .

  • @shrutykhullar1978
    @shrutykhullar1978 4 роки тому +1

    thanks a lot for awesome explanation!!!saw many videos but the concept was not cleared.but your video made it very easy

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

      Checkout free course - cb.lk/cpcpli for amazing videos.

  • @rohanpatil8417
    @rohanpatil8417 6 років тому +5

    Do More, Eagerly Waiting !! AND also please publish videos on solutions to problems on Hackerank, Codechef ,etc...That Would Really Help !

  • @carnyx9784
    @carnyx9784 4 роки тому +1

    i really appreciate your work, the way you write coding was awesome keep up the good work.

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

    I feel lucky today coz when I liked your video it changed from 1.4k to 1.5k👍 indeed best explanation .... Thank you very much Sir

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

    Great and explicit explanation. Thank you so much! Helps a lot!

  • @manojnaidu619
    @manojnaidu619 5 років тому +4

    Explanation is best in class!

  • @ladislavszabo4247
    @ladislavszabo4247 4 роки тому +1

    Thank you very much for this video sir! Great explanation!

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

    very nice explanation sir , thank you very much!!

  • @abhishek_bhardwaj
    @abhishek_bhardwaj 6 років тому +16

    Very nice... Please make more videos on data structures and algorithms...

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

    Very clear explanation. THANK YOU!

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

      Thanks Bhargavi ! Pls subscribe and spread the word about our channel with your friends.
      - Prateek Bhayia

  • @azzie7691
    @azzie7691 6 років тому +2

    Loved it👍

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

    Thank you so much for coding merge sort, I struggle understanding the code in the book

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

    Thank You Sir !!
    Very helpful...

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

    omg!!! you are soo good!!!!! excellent explanation....keep it up!!! and thankyou soooo much!!!

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

      Thanks Athira ! Pls subscribe my channel and share with friends. - Prateek

  • @vanshkamdar149
    @vanshkamdar149 4 роки тому +1

    Great job bro finally i understood

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

    Excellent explanation !!!!

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

    best explaination i think

  • @sathiyaprakash1012
    @sathiyaprakash1012 5 років тому +1

    Best explanation!

  • @areebamughal750
    @areebamughal750 5 років тому +1

    thank you so much bro...

  • @AkshitArora
    @AkshitArora 6 років тому +2

    awesome!

  • @ketulchauhan454
    @ketulchauhan454 4 роки тому +1

    Superb Explanation Buddy Really Appreciate Your Work Thanks A Lot 😊👍👍

  • @tincuptan
    @tincuptan 4 роки тому +1

    In 11:45 in the merge function just change the logical and with logical or then you don't have to write those extra while loops for one array exhausting

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

    Nice explanation thank u

  • @divyatejaswinivengada6368
    @divyatejaswinivengada6368 4 роки тому +1

    Thanks man awesome explanation

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

    Quick question: Is the space complexity of your merge sort code is O(nlog(n))? n because of the temporary array and log(n) because of the recursive calls? Or would it be O(n) because of the temporary array? Thanks

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

    thanks for the video
    it's a nice explanation

  • @AnkitSaiyan
    @AnkitSaiyan 5 років тому +10

    You should explain how that recursive function worked in this case .... Basic functionality of merge sort is easy to understand but hard to code. I still didn't get the recursion calls

    • @nikhilbisen25
      @nikhilbisen25 4 роки тому +1

      this is what exactly I came here for, I have been struggling to understand the mergesort function, as to how just bypassing left right and mid it will relate to index of the array. if u found out ans pls reply

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

      Same problem, even I didn't get how that recursive function work.

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

      @@MrDaksha77 I will explain plz watch.

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

      Watch this video
      Topic Merge Sort
      ua-cam.com/video/4-J3ocFd98k/v-deo.html

  • @barshatewary9547
    @barshatewary9547 6 років тому +2

    Best of all...

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

    Very nice explanation. Feels easy to understand. Main thing is that you have shown how to write the program for it. Many of the videos on UA-cam have just given theorotical explanation. 👍👍

    • @CodingBlocksIndia
      @CodingBlocksIndia  4 роки тому +1

      Thanks Gauri. You can find more detailed videos on online.codingblocks.com

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

    really great video! thanks!

  • @RajSingh-gy8pu
    @RajSingh-gy8pu 6 років тому +1

    nice.......please add a video on implementation of bubble sort on linked list and more on data structures like trees..!!

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

    Really nice explanation ...Tq so much

  • @mrs.confetti
    @mrs.confetti 3 роки тому

    Best explanation! thank you so much

  • @tushargoyaliit
    @tushargoyaliit 5 років тому +1

    Awesome

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

    awesome thankyou!

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

    Bro u explained very well such lot of teacher teach like different behaviour u are awesome bro I got it this code how can we implement this thnks.

  • @XDavidT5
    @XDavidT5 6 років тому +2

    3 first lines
    need to fix:
    int a[n];
    after you get n

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

    Thank you sir

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

    Thank you vary much

  • @tushargoyaliit
    @tushargoyaliit 5 років тому +1

    loved

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

    Can the elements be directly transferred in array a without using the temporary array?

  • @fatamajannattisha68
    @fatamajannattisha68 4 роки тому +1

    thank you so much brother

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

    thank you

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

    thankyou so much
    🙌

  • @DG-df3co
    @DG-df3co 4 роки тому +1

    very good explanation! greetings from Germany :)

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

    Why do we need to to pass 's' and 'e' arguments seperately to the functions? Doesn't the the array already have information about its start and end? Just curious to see how can we approach this problem by just passing array to a single function rather that two functions.

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

    I am implementing same code but not getting the same answer my answer is something -346521

  • @MustafaKHAN-sw1xb
    @MustafaKHAN-sw1xb 6 років тому

    this material provide in hindi so very helpful for student

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

      will do in Hindi as well. Keep watching.

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

    nice

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

    Hello sir..i have a doubt tht why did u called merge(a,s,e) as already u gave it by dividing the array.then why did u call that above function..explain it sir

  • @nakibtheexplorer5383
    @nakibtheexplorer5383 5 років тому +1

    Best

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

    One of best

  • @shubhamkant_
    @shubhamkant_ 4 роки тому +1

    💯💯

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

    Why is the size of temp array fixed to 100? What if the number of elements are more?

  • @gameplayandreview
    @gameplayandreview 5 років тому +3

    Very low sound while you are explaining, in starting volume was loud and clear!

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

    in merge function in parameters , why did you wrote int *a, why not int a[ ]?

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

    its 2 am and thank you very much

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

    5:00 I understand why we need the base case in the code, but I do not understand how that if statement reflects what we are trying to do at all. if (s>=e), then return . So for example, if the starting point was 4, and the ending point was 1 (so in this case the starting point is greater than the end point) why the hell do we NOT want to sort it, and why do we want to return it. Makes no sense, since we want the array in increasing order.

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

      no the start and end point refers to the index of the array.... so when s=e then we dont divide it further....

  • @chetankhandelwal8811
    @chetankhandelwal8811 5 років тому +1

    what if elements of array are in odd no?then how to calculate mid point

    • @AnkitSaiyan
      @AnkitSaiyan 5 років тому

      Since the container is of int type... So 5/2 will return 2 instead of 2.5. so 2 will become the center meaning out of 5 elemnts left part will have 2 and right part array will have 3 elements

    • @AnkitSaiyan
      @AnkitSaiyan 5 років тому

      Since the container is of int type... So 5/2 will return 2 instead of 2.5. so 2 will become the center meaning out of 5 elemnts left part will have 2 and right part array will have 3 elements

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

      Mid = s + e-1/2

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

    I think it's pretty sad that almost no one makes mergesort that returns a list instead of a void.
    not changing input is good practice

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

    My left ear understood it😅🤣

  • @ashirbad29
    @ashirbad29 4 роки тому +1

    can anyone explain to me why he use that *a pointer in place of the array as a parameter in marge function

    • @KM-sf6zy
      @KM-sf6zy 4 роки тому

      you can use the array. both can be used interchangably

    • @ashirbad29
      @ashirbad29 4 роки тому +1

      @@KM-sf6zy thanks..

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

      @@ashirbad29 Both does the same work , putting an int arr[ ] , this will prompt the function to access the memory where int arr [ ] is stored, C++ does this without showing you so in background , if we use pointer *arr , then it directly points to the memory where the array is stored so the function can access it.

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

    Sorting kyse ho rhi h

  • @GogCode
    @GogCode 5 років тому

    you are legend bro

  • @MultiSilko
    @MultiSilko 5 років тому

    I couldn't find the code there so can u please write it in the comments?

    • @CodingBlocksIndia
      @CodingBlocksIndia  5 років тому

      Please check the description to get source code ☺️

  • @bobbytop6109
    @bobbytop6109 5 років тому

    In main why why do we pass (n-1) to mergesort why dont we pass n?

    • @raghavrajagarwal7074
      @raghavrajagarwal7074 5 років тому

      since the last index of array is at 'n-1' position not on 'n'. The 'n' is total length but there are 'n-1' indexes as array starts from 0.

    • @bobbytop6109
      @bobbytop6109 5 років тому

      @@raghavrajagarwal7074 ok so while comparing array we do it till n-1 and not n for all cases??

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

      @@bobbytop6109 because array starts from 0 not from 1... so array size is n-1

  • @tohelchini2830
    @tohelchini2830 5 років тому +1

    Thanks quick sort plz

  • @natashatanzila1520
    @natashatanzila1520 5 років тому +1

    #include
    using namespace std;
    void emerge(int *a,int s,int e)
    {
    int mid=(s+e)/2;
    int i=s;
    int j=mid+1;
    int k=s;
    int temp[100];
    while(i

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

    Hates off ❤️👏

  • @physicswithneerajbhaiya
    @physicswithneerajbhaiya 5 років тому +1

    sir pls use mic, background noise is hurting ears

  • @subhasmitajena926
    @subhasmitajena926 5 років тому

    Nice but more explanation is needed.

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

    Please provide these videos in hindi.

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

    Become a professional programmer with Coding Blocks' Online Courses. Visit cb.lk/2T7WP to explore all courses. Use Coupon Code "UA-cam" to avail 25% Off on Our Courses.

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

    Mozart algorithm? ;)

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

    its really sad, that most of those youtube videos are with strong accent, where you understand like 5% of anything.

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

      That's bs.

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

      ​@@oadka yeah, your comment is BS. especially when i look at your "native" level of english in your videos. so shut up on the cheap seats.

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

      @@silenthive Hey hey free views for me! 😁

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

      @@silenthive yeah man, i mean, He spent his time to explain this for free so why being so lazy to take some extra english classes so you can be pleased right? 🙄 Just shut up and be grateful for the time the guy took to explain it. This was clearer than any of the other explanations i found.

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

    this code couldn't be run on hackerrank.com!!!!!!!!!!!!!!!!!!!!!!!!!!!!! so please sir , try with better approach!!!!!!!!!

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

    Good tutorial. Much appreciated!!

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

    Poor audio

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

    i am thinking about students who paid for coding blocks courses. it's worse than random youtube video

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

    nice