Merge sort algorithm

mycodeschool · 2.3M views · 2013 · 18:20

Open on YouTube

Algorithms & data structuresEducation & learningtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when beginners are confused by merge sort recursion, the merge step, or the leftover while loops.

Recommended by 4 people across 4 different months, 2015 to 2024.

What it is

A mycodeschool lesson that explains the merge sort algorithm, a divide and conquer sort with worst case O(n log n) time. It walks through the recursion and the merge step, with a link to a real implementation. It is part of the channel's sorting algorithms playlist.

What people said

Recommends user4398985 · 2015-09 · Stack Overflow · on “Merge Sort Recursion”

This is beginner's way of thinking. Yes, exactly I thought the same when I encountered this before. I couldn't believe that the same array size can change dynamically. Understand this, in the below code, array l and array r are created with different sizes for every recursive call . Don't confuse on this. Yes, this is never possible that the same array size changes dynamically for a beginner like you and me. But, there is an exception, well, there are exceptions. We will see them very often as we move forward. Its recursion, in recursion things change dynamically and all this changes are stored in a call stack. Its confusing but its really interesting if you ponder over it. Its profound. Mer

Recommends sergih123 · 2019-01 · Stack Overflow · on “Sorting an array from lowest to greatest FORTRAN”

Thank you guys, and sorry for this being a vague and confusing question, I have noticed and will not make the same mistake in the future. I',ve decided to go for the merge method of which I found a great video at [this video]

Recommends Rohith V · 2021-07 · Stack Overflow · on “Recursive call in merge sort confusion”

We don't want to return any array or list here as we pass the array and it gets changed after every recursive call in place. You can visit this link to know more about mergesort : Link [this video]

Recommends Margret Mauno · 2024-04 · Stack Overflow · on “Purpose of last 2 while loops in the merge algorithm of merge sort sorting technique”

I have been able to see the reason for the 2 extra while loops. The answer I have accepted above is accurate. But if you still do not understand the reason for the two extra while loops, I recommend looking at this video: Merge sort YouTube video [this video] (Especially from around minute 6:30-7:00) thereabout. I feel that this answer will be helpful for beginners like me. I shall find time to edit this answer and explain what I understood in detail.