Merge Sort Using Recursion (Theory + Complexity + Code)

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

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

  • @KunalKushwaha
    @KunalKushwaha  3 месяца назад +4

    DSA + interview preparation playlist: ruclips.net/p/PL9gnSGHSqcnr_DxHsP7AW9ftq0AtAyYqJ

  • @sarikarai9241
    @sarikarai9241 3 года назад +142

    Only one request is... Please Do complete this playlist ( in whatever tym) just don't leave in between. It's the best course ever.

    • @KunalKushwaha
      @KunalKushwaha  3 года назад +103

      I will complete

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

      @@KunalKushwaha when?

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

      @@SunnyGupta00in October

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

      @@vdas1335 not possible

    • @dark_techyy
      @dark_techyy 3 года назад +12

      dont worry nhi chhodega vo...he is lion 🦁 😅

  • @GoodBoy_._
    @GoodBoy_._ 5 месяцев назад +5

    for those who found time Complexity difficult to understand I think I can Help -->
    1.Total no .of recursion calls = logN
    2. No of numbers to merge in each recursion calls = N
    Time COmplexity =O(N*logN)

  • @rishabhmohan_
    @rishabhmohan_ 3 года назад +18

    Undoubtedly...best ever course for DSA on RUclips.
    Thanks Kunal, for giving this level of content for free. ❤️

  • @MIHIRHUNDIWALA
    @MIHIRHUNDIWALA 2 года назад +29

    For those who are wondering about stability, the version of mergesort implemented in the video is unstable, but a simple tweak can make it stable
    changing the condition to first[i]

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

      What's a stable algorithm?

    • @capedbaldy123
      @capedbaldy123 10 месяцев назад +2

      ​@@_DashingAdi_ suppose there are multiple same element/number in array, their positions will be preserved as it is.. without changing it...
      suppose [2(1st), 2(2nd), 2(3rd)] their positions will be preserved as it is...

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

    Probably you're the best teacher in my whole life that I am able to understand everything without asking much questions.
    Simply you're the best

  • @mdmozammilraza3215
    @mdmozammilraza3215 3 года назад +145

    Best explanation than other RUclipsrs. Hope you will upload videos as like that will help us in competition programming.

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

      Which language is he using ??

    • @prajwalm.s7976
      @prajwalm.s7976 3 года назад +6

      @@tasneemsf3421 Java

    • @mdhaidarparwez968
      @mdhaidarparwez968 Год назад +6

      Inplace merge sort is incorrect,he is still using extra array,how he can say this is inplace,if you don't know inplace sorting ,why you teach wrong concept?

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

      @@mdhaidarparwez968 exactly

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

    It's the BEST course ever , i recommend everyone to watch it

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

    I don't know how to explain this but when I am stuck on a uni task, I get a weird sensation of safety watching your videos as I know if I understand your video thoroughly, I will get the answer. Thanks.

  • @ujwalapatil650
    @ujwalapatil650 3 года назад +6

    Words are limited to appreciate this bootcamp!! Nice bro Thanks

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

    Words are not enough to appreciate your way of teaching.

  • @49-farhaanali86
    @49-farhaanali86 6 месяцев назад

    Exceptional,this guy deserves the best award for dsa

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

    Thanks for teaching us like no one did till now!! One small request, please make lectures on dynamic programming as well!!

  • @sammedsankonatti9642
    @sammedsankonatti9642 2 года назад +6

    Even after watching so many videos on merge sort i was in confusion, but this video alone did wonderful job to clear so many or all the doubts🤩,
    Thank you and keep going.

  • @luisquiroz8586
    @luisquiroz8586 2 года назад +9

    @Kunal Kushwaha In the sort in place code your base condition is if (end - start == 1). why is this the base I thought you wanted to return when you only have one element. but in your case you will have 2 elements because if end is 1 and start is 0 => 1-0 = 1 but that's 2 elements 0 and 1 so why is that base?

    • @charname2077
      @charname2077 Год назад +3

      Yeah he made a mistake there. This code will fail for an array like [2, 1]

    • @sufiserious798
      @sufiserious798 10 месяцев назад +7

      The key to understanding the reason is at the start of the code, where the function is called for the first time.
      There Kunal passed '0' as start and 'arr.length' ( 1 so the algo will proceed ahead to next call
      In the next call we will have arrays containing only a single element for example {2}
      Then start is 0, end is arr.length that is '1' , not '0'.
      Now, our array contains only 1 element and (end - start) will be (1 - 0) which is equal to 1!!
      Thus, the single element arr will be returned directly above !

    • @nafisaparveen9759
      @nafisaparveen9759 24 дня назад +1

      ​@@sufiserious798thanks a lot!

  • @Victor-wy1wj
    @Victor-wy1wj Год назад +2

    The explanation is on the next level. I finished the second year of college of Technology and Informatics and those topics were hard and weird to me but now while i'm in vacation i decided to give it a second chance , to try to understand them finally because it's a really important topic. Thank you so much Kunal for your explanation and that all of this is free.

  • @youmadvids
    @youmadvids 2 года назад +11

    Your vids are awesome man. This is exactly how algorithms should be taught. I've got my final interview with Microsoft tomorrow, wish me luck!

    • @KunalKushwaha
      @KunalKushwaha  2 года назад +8

      Best of luck!

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

      were u able to crack it ?

    • @youmadvids
      @youmadvids 2 года назад +12

      @@sofiyarao815 I got 4/5 working solutions, unfortunately didn't get the job :(( but its all good practice!

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

      @@youmadvids now whats your current status man

    • @nafisaparveen9759
      @nafisaparveen9759 24 дня назад

      whats up?? where are you woking now?​@@youmadvids

  • @veeee5577
    @veeee5577 2 месяца назад

    i wish i had your explanation skills you make it look so easy

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

    best DSA course ever💯💯💯

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

    Hi Kunal, Thanks for uploading the video .
    Uploaded video are helpful and explanatory videos for fresher and experience folks.
    I have a suggestion for you. Please upload a video for Thread class ,Multithreading, thread pool etc..

  • @sofiyarao815
    @sofiyarao815 2 года назад +6

    Kunal in every video -> recursively stating = Already explained in prev videos please watch ...

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

    I saw a dozen of merge sort videos but did not understand. Finally, your explanation went straight through my head and not a bouncer :)
    Thanks a lot for this amazing video !

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

      Kunal is great but imo his explanation is spoon feeding I was not able to think on my own after his tutorials...Try Aditya verma videos after conpleting topics he helped me alot with thinking approach

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

      @@rjesh2062 its not like spoon feeding. but we can say that his approach is addictive and even better than others.

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

    The Best Explanation of MergeSort !

  • @Content_creater_entertainment
    @Content_creater_entertainment 2 месяца назад +1

    Kunal you are a gem man

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

    Thanks for this video. Now I don't have any confusion in Recursion.

  • @suryaer3369
    @suryaer3369 3 года назад +12

    Please complete this series boss , it gives me panic once you give some gap in between 😄😬

  • @samarthdhawan2229
    @samarthdhawan2229 2 года назад +6

    At 29:15 you said there are N elements in the stack memory but there should be atmost
    logN elements in the stack memory as it is the height of the recursive tree......Btw thanks for this amazing DSA series it really helps me a lot

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

      O(log n): If you will declare the intermediate array in heap.
      O(n): If your intermediate array is stored in stack.

  • @Vaishnavi0810-j3i
    @Vaishnavi0810-j3i Месяц назад

    Thank you brother ❤.... please complete the playlist

  • @antoniokhan9367
    @antoniokhan9367 3 года назад +6

    Kunal on Fire!

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

    Merge sort by changing pointers of index value:
    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int arr[]={5,4,3,2,1};
    merge(arr,0,arr.length);
    System.out.println(Arrays.toString(arr));
    }
    public static void merge(int arr[],int s, int e){
    if(e-s==1){
    return;
    }
    int m=(s+e)/2;
    merge(arr,s,m);
    merge(arr,m,e);
    int ans[]=new int[e-s];
    int i=0,j=s,k=m;
    while(j

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

      while copying the answer of mix in original array why do we have to take arr[i+s]?

  • @CoolCircuits
    @CoolCircuits Год назад +7

    37:45 There is Not arr,m,end this is arr,m+1,end

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

    Recursion and how recursion worked lay hid in night; God said, "Let Kunal be", and there was light!

  • @ishanvyas2178
    @ishanvyas2178 4 месяца назад +3

    need to watch again it's already 11 at night and feels like overdose

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

    forever grateful to you kunal!!!

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

    Just a single word for you " Legendary " !!!!!!!

  • @NareshNaresh-cl8rn
    @NareshNaresh-cl8rn 5 месяцев назад

    Kunal alpha character explanation I doesn't think anyone who can teach like him

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

    Exceptional quality of teaching and content delivering.

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

    Which one should we use if asked in an interview inplace or normal mergesort

  • @kunalchauhan5294
    @kunalchauhan5294 2 года назад +2

    Not gonna to Lie You are Really A King 👑
    Not just like this dude You blew My mind
    You are fantastic my Dude
    You just made everything so Simple

  • @jagadishrvjp4450
    @jagadishrvjp4450 Год назад +6

    My doubt is that the mid index is passed twice ( in left array, as well as in right array ), won't this repeat the number in the final array?
    I was wondering whether the right array's mid should be passed as mid+1 or something like that?? Anybody please clear my doubt!

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

      Found it: Basically, when you copy the range of array, the last element is exclusive (i.e) when you pass Arrays.copyOfRange(arr, 0, mid); // mid is excluded!
      and when you pass Arrays.copyOfRange(arr, mid, arr.length) // mid gets included here, and arr.length = 5, so 5th index is excluded which doesn't exist as the index starts from 0, so last element is basically index 4, which is also covered !

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

      can you explain these case with the inplace method@@jagadishrvjp4450

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

      ​@@jagadishrvjp4450But even while using InPlace sorting (where he did not use .CopyOfRange) he still passed mid two times.

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

      @@jagadishrvjp4450 Thanks man! Had the same doubt

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

    Best explanation ever. Thank you

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

    one of the finest in explaining things 👌

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

    east and west,kunal is best

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

    Debugging by my self helps me a lot 👌.

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

    Hey Kunal, This is the best course I have came across. But I just wanted to ask you when will the course finish?

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

    Very good and in depth explanation of the algorithm... Good Work 🙂

  • @btsworld3739
    @btsworld3739 3 месяца назад

    kunal bhaiya, when are you going to complete this series? i am desperately waiting for dp and graph videos..

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

    Hey Kunal, you missed some conditions in merge sort code in function merge(), while(k < first.length + second.length -1 && i

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

    everything is easy with ur explanation except the math part for me coz no clg need to learn calcu! great vid btw

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

    Amazing Videos, I have been learning a lot from these! Please also explain Trees, Graphs & DP.

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

    Eagerly wait for it kunal

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

    I can say proudly that this is the best DSA course on youtube.
    Thanks Kunal

  • @girirajtomar519
    @girirajtomar519 2 года назад +2

    Amazing lecture brother.

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

    Hi Kunal pls also do the videos on applitude and reasoning it will help ful..

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

    when e-s ==1 .. the array will be having 2 elements.. how that is working properly?

  • @suyashkumar8840
    @suyashkumar8840 3 года назад +13

    Love Babbar be like jldi jldi one shot recursion bna ke bacho ko geekforgeeks ke course bech deta hu 😂😂

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

    Your videos are amazing 👍

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

    Thanks for this amazing explanation
    I really like it 🤗❤‍🔥🔥👍🏻

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

    Great video! Thank you very much Kunal!

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

    hey kunal when u say "I am not going to teach u these easy stuff again" we actually get a recap of your previous concepts.

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

    Hey kunal !!.. When ur going to complete entire lectures of DSA boot camp? Needed much : )

  • @Dekh_rha_hai_vinod.
    @Dekh_rha_hai_vinod. 3 года назад +3

    Brooo suppper👌👌👌👌👌👌👌

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

    Coming here after watching your video on Recursion ( makes it so easy to understand ) 😂

  • @akshaykumar-wd8jc
    @akshaykumar-wd8jc 3 года назад +1

    Great explanation

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

    Bro when will be this dsa playlist will be completed?

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

    Great video! I have one question: for the mergeInPlace function, it created a new array int[] mix = new int[e - s]; --> so is it still considered in place?

  • @vasujhawar.6987
    @vasujhawar.6987 2 года назад +1

    long time, missed you.💗💗

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

    why do we use Sorting algorithms when we have a direct method which is Arrays.sort() 🙄 ?

    • @KunalKushwaha
      @KunalKushwaha  2 года назад +16

      lmao, .sort() also uses these methods. its not magic.

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

    Video 30 Completed!

  • @Aashutosh_Bajaj
    @Aashutosh_Bajaj 3 месяца назад

    (17:40) I did not understand why you used the copyOfRange Method?

  • @HarshGoel-m5c
    @HarshGoel-m5c 4 месяца назад +2

    I like your videos bro but,
    the merge sort in place code is wrong, you do not use that extra array in merge function, it defeats the whole point of taking indices and stuff

  • @rohitmondal3438
    @rohitmondal3438 2 года назад +2

    I've used the same program as of you of in-place algorithm, then why it's giving wrong answer ??

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

      Because the algorithm is full of bugs

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

    best sir in world

  • @tishachoudhuri5721
    @tishachoudhuri5721 2 месяца назад

    I enjoyed this video. I just did not understand the part where mid turned out to be exclusive. so I did a mid+1, only half the sorted array is getting picked. If any one understands, kindly explain :)

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

    Hey Kunal, How your are using debugger in such a way that it is showing each and every step....is any extension installed or just using F5

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

    still watching consistently
    😀😀😀
    😀😀

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

    great video bro, thank you so much!!

  • @abhijitmanna4524
    @abhijitmanna4524 2 года назад +2

    This time complicated mergeSort into mix etc it should have been taught in simple method.

  • @Memes_Karaa
    @Memes_Karaa 2 месяца назад

    what if k=s in the in-place merge sort we dont even need the mix[] we can use arr[k]

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

    thank youuu;)

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

    All the things kunal explained was so much clear but I have a doubt about why is he using arr.length and not arr.length-1 in the in place merge sort part?

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

      same.. have you figured it out?

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

      @@immortal4412 I know it's late, but in JAVA, arr.length returns total number of elements(1 indexed), not the SIZE of the array(0 indexed), unlike C++, example: {1,2,3,4,5} n = 5, but 5 is the 4th element.

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

      @@wegojim732 arrays are 0-indexed in C++ too, that is not the reason for using arr.length

  • @CineSavvySoul
    @CineSavvySoul 2 года назад +2

    first time kunal sir gets confused🤭🤫

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

    Thank for the lecture. My support through subscribed already 😊

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

    how tf every kunal is genius

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

    Can you please complete the whole bootcamp.

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

    Here your are using copyofrange method for java like same thing how to do in python?

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

    When copying elements of mix[ ] to our arr, why are we saying arr[s + l]? Can't we say arr[l] = mix[l]?

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

      Because the start varies for each recursion call.
      If we're in the last call the start can be somewhere like index->4 so if we use arr[l] the elements will get copied from 0 instead of the particular start index of that Recursion call.
      Using arr[l] will get the elements copied from 0 at all time regardless of which call hence it's wrong.
      When we use arr[s+l] we will copy the mix of that particular recursion call at the start of the array of that recursion call.

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

    iterative code for merge sort - bottom up approach
    static void iterativeMergeSort(int[] arr){
    int sizeOfImgArr = 1;
    for(sizeOfImgArr = 1; sizeOfImgArr

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

    Number of comparison will not increase more then n- 1 ??

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

    how is it going to be inplace mergeSort , if we are creating a new array in the mergeInPlace( ) method??

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

    sir their is problem in printing the arrey in main class , because the syntex you give in github and the syntex in your video is different .

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

    thankyou brother

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

    Bro please complete the series

  • @aryanverma1233
    @aryanverma1233 Год назад +2

    How after dividing arrays in halfs those halfs are getting sorted?

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

      Same doubt dude😭

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

      same same hahaha'
      @@plutomessi21

    • @ARYANShukla-c5u
      @ARYANShukla-c5u 11 месяцев назад +1

      we divide arrays until its size become 1.
      when we merge both arrays of size 1 then it likes comparing two numbers.

  • @piyushsukhwani-qm2pn
    @piyushsukhwani-qm2pn 7 месяцев назад

    what about comparisons made in copying of array in method "copyofrange"????

  • @dineshmadduru7043
    @dineshmadduru7043 5 месяцев назад

    Thank You :)

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

    42:36 broo plz clarify this part more

  • @VirajKhot-z8m
    @VirajKhot-z8m Год назад

    Why didn't you put 'return' while creating left and right arrays? There must be return when function has a datatype right?

    • @ItxZubair-g6u
      @ItxZubair-g6u 3 месяца назад

      because he saving the answer into keft or right variable arrays so instead of direct return you can save it in a variable

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

    pretty cool stuff

  • @suvraneelsaha8973
    @suvraneelsaha8973 3 года назад +6

    hey kunal i need your advice . im following your course and im loving it , but the thing is recently i was thinking of taking a coding ninja dsa + java course . read the reviews but cant make up my mind . i dont have much time left im already in 3rd year .
    please guide me
    would love to have advices and insights from everyone .
    thanking you in advance

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

      Is this course not enough to crack interview

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

      Kunal's course is enough bro, but if you still want to spend money on some course go ahead you will learn after that why this course is best. 😂

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

      @@rishika9102 to be honest im in the 3rd video ,i just cant make my mind straight

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

      @@blank2782 i dont know man i kinda in a dilemma , i just dont know what to do . for the past 3 years i have randomly scrolled and watched youtube . it was a havoc and now i just dont know what to do . and there is no time left

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

      @@suvraneelsaha8973 ya that's the same for me ..