mergeSort(): A Graphical, Recursive, C++ Explanation

Поділитися
Вставка
  • Опубліковано 18 жов 2024

КОМЕНТАРІ • 68

  • @burakbiyikli
    @burakbiyikli 4 роки тому +9

    //This is an annotation of the code that they provide for merge.
    void merge(int A[], int low, int high, int mid)
    {
    //We meed c[50] to put our sorted data into. This is as we need to maintain the order of A[] until the end of the function
    //A[] is passed by reference as its an array (pointer), so in this application all merge() calls share A[].
    // I & J are indexes for each half of the array section to be merged. In this function I & J will move right.
    // k is the index to which we are writing to in the temporary array c[50]. (This merge will not work on array size > 50 lol.)
    int i, j, k, c[50];
    i = low;
    k = low;
    j = mid + 1;
    //We have two sorted subsections in merge sort, so we need to determine which values to copy, take the example of:
    // [0,2, 1,3] where each half of the array are independently sorted. here we can merge the array by looking at the beggining of each
    // section and only reading the next value of a section if the last value read was lower. IE. read 0 and 1, 0 is lower so we write 0 to our result. r = [ 0 ? ? ? ?], then we read 2 and 1, 1 is lower so we write 1 and r = [ 0 1 ? ?], then we read 2,3 and 2 is lower so we write this to our result r = [0 1 2 ? ] then we have 3 left which this while loop doesn't handle, but the next does.
    while (i

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

      You're a fucking champ 🍻

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

      One question though, this doesn't seem to work for uneven arrays. Why would that be?

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

      @@sidiousvic Sorry but I am not quite sure I follow what you mean by uneven. The main requirement of the code is that both regions given to merge are sorted. The code should run if you give it uneven arrays as I will explain, but in the execution of the algo. as shown it is called with typically even arrays as inputs.
      Given this The function should work even if we feed it two sections containing [1,5] and [2,3,4,6,7,8,9] as the first loop terminates once c looks like [1,2,3,4,5] or so the second loop is skipped and the third loop moves the remainder such that c looks something like [1,2,3,4,5,6,7,8,9]
      It has been a bit since i looked at it closely, but as the first loop does not really 'take turns' and the other two loops are for handling excess I believe the code should run on uneven sets. So the first loop runs until only one array is left then one of the remaining loops copies the rest from that array
      The biggest Issue I have with this code is how it allocates memory that prevents it from sorting any set of a large size

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

      I wish this comment achieve more likes than the video itself

  • @tenzin8773
    @tenzin8773 6 років тому +12

    I was scratching my head about how the recursive actually work for this algorithm. Now, it is clear. Thank you!

  • @Daniel-sy5wz
    @Daniel-sy5wz 4 роки тому +15

    Thank you very much, two recursions in one function make me confused a while. This video is fantastic.

  • @lipishah474
    @lipishah474 5 років тому +31

    Excellent interpretation of recursion with stack visualization.

  • @gabrielumana5762
    @gabrielumana5762 3 роки тому +3

    I was struggling to do the merge part until I found your video and understood how to do it. Thanks a lot, really! And just keep coding :)

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

    Best representation of call stack for merge sort. Much appreciated.....🙌🙌

  • @tarekshokry1366
    @tarekshokry1366 11 годин тому +1

    Great video, thank you very much ❤

  • @lenzpaul2205
    @lenzpaul2205 5 років тому +42

    Doesn’t explain how the merge function does the actual sorting ...

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

      Yeah wtf

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

      I already know how merge sort works, just forgot how to merge. I was watching this video and expecting he to explain merge() so honestly I was a little bit triggered when he just showed that "magic" gif.

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

      This is video is only for mergeSort visualisation.

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

    Thankyou very much. It helped me a lot.

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

    Thank you for the video! Its visualization really helps for better understanding of recursion.

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

    This is one of the best tutorial videos I have ever seen - thanks to you I got to understand the recursion as well as the merge sort algorithm, so you killed two birds with one stone. Great work!

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

    The video explanation is phenomenal just which call stack.

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

    this video should be on top because other are just explainging code and i wonder how htey even explain when they didnt understand as well so this video should be on top

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

    Lets just all appreciate how CONCISE this code is compared to other merge sorts.

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

    Thank you very much ❤ ... Excellent interpretation.

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

    Front end trying to dive into back. This video helped.

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

    What happen when you divide an array? Does it makes two different new arrays everytime with splitting elements? Does it mean we are assigning every splited to a new array?

    • @tarekshokry1366
      @tarekshokry1366 11 годин тому

      Yes we divide each array into 2 different arrays

  • @Forlorade
    @Forlorade 6 років тому +4

    Great visualization, thank you, sir.

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

    Thanks a lot! Finally understood how the recursion actually works!

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

    Thank you so much! This was really helpful!

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

    wow good work.you visualize how recursive work.thank you

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

    thanks alot sir........ your learning is incredible 😍😍

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

    When the mergesort() on left statement returns then it should call mergesort on right part naa?
    Why it calls merge() instead of mergesort() on remaining right part of array?

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

    Awesome tutorial!

  • @Int3rSys
    @Int3rSys 7 років тому +20

    I am sorry but this is not called an explanation. It's a gentle introduction...

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

      Why is this a gentle introduction and not an explanation?

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

    thank you so much, it's helpful

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

    Don't understand how that final two sub arrays suddenly get merged together sorted

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

    i want to know ;how exactly does the merge method works?

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

    May i know how u did these graphics?

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

      PowerPoint! It’s a very accessible solution for things like this, but admittedly annoying to record over.

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

    Thanks !!Great VIDEO.... It's really easy and helpful to understand how recursion works . Pls keep uploading like this>>>>>

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

    sorry but how I find steps? when teacher said write the steps which line is step?? he gave the number of example

  • @kloud..
    @kloud.. 7 років тому

    Can you post the merge(A, low, high, mid) function code in the description? I want to see the logic for the merge function

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

    thanks for this video with stack. i get it !:)

  • @cosmology4168
    @cosmology4168 7 років тому +4

    I am not understanding the recursive call.say when merge_sort(low,mid) is called until low ==high ,reaching a single element,
    Then merge_sort(mid+1,high) is called and then mid+1 will be 1 like merge_sort(1,high) bcz from previous recursive func we get mid as 0.
    And this where I am confused about how it does really execute.

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

      i have the same doubt........can you please explain if the doubt is clarified by now?

    • @Namesake..
      @Namesake.. 6 років тому

      at the end of the first recursive call i.e., when low = 0, high = 1 and mid = 0, the function returns and the next mergesort for the second half is called. Remember at this point the values are low = 0, high=1 and mid =0 so the call would be mergesort(mid+1, high) = mergesort(1, 1) which would immediately return since 1 = 1.. and this follows.

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

    muchas gracias y buena explicacion.

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

    very helpful,thanks a lot!

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

    which tool did you use to create these graphics?

  • @Пятница-ю7ц
    @Пятница-ю7ц 4 роки тому

    how to run this code

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

    can anyone explain why h is array size-1?

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

    Can I please get the source code of the demonstration? I needed it for my college project

  • @pulkitchauhan6602
    @pulkitchauhan6602 Місяць тому +1

    good

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

    well done!!

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

    This is great, thank you!

  • @Marwan-oh4tk
    @Marwan-oh4tk Рік тому

    ty

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

    please give some codes to practice with and be smooth with it

  • @Isheel
    @Isheel 7 років тому

    great work sir

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

    interesting ...
    even this person is saying "sort" as "sword"

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

      You need your ears checked mate.

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

    Your voice is like Mark zuckerberg xD

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

    lol "Just keep coding"
    Will do.

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

    genant

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

    This video is terrible