Merge sort
Let’s start with an unsorted list
Divide the list into two lists
Divide that list up again
Divide that list up again
And again...
A single item list must be sorted✓
go to the next list, a single item list must be sorted✓
jj
ii
KK
Now take that pair and merge
jj
ii
KK
Look down both lists taking the smallest
jj
ii
✓
KK
When both lists run out you have a sorted list
jj
ii
Now this pair is sorted
✓ Do the same with the next pair
✓ Each is a list of one item (so sorted)
jj
ii
KK
We merge again
jj
ii
KK
Creating a new sorted list
Now we have two lists of pairs...
So we merge them
jj
ii
KK
Look down both lists taking the smallest
jj
ii
KK
jj
ii
KK
Look down both lists taking the smallest
jj
ii
KK
Look down both lists taking the smallest
jj
ii
KK
Look down both lists taking the smallest
jj
ii
KK
Look down both lists taking the smallest
the list is sorted and you can begin the next quarter
A single item list must be sorted✓
A single item list must be sorted✓
jj
ii
KK
Merge this pair
jj
ii
KK
Pick the smaller item
jj
ii
KK
When both lists run out you have a sorted list
Sort the next pair
A single item list must be sorted
✓
A single item list must be sorted
✓
Merge the single item lists
jj
ii
KK
Merge this pair
jj
ii
KK
Pick the smaller item
jj
ii
KK
Pick the smaller item
Now we have two lists of pairs...
ii
jj
KK
So we merge them
ii
jj
KK
Pick the smaller item
ii
jj
KK
Pick the smaller item
ii
jj
KK
Pick the smaller item
ii
jj
KK
Pick the smaller item
the pairs lists are now merged
ii
jj
KK
Now we merge the quarters
ii
jj
KK
Pick the smaller item
ii
jj
KK
and move on
ii
jj
KK
Pick the smaller item
ii
jj
KK
and move on
ii
jj
KK
Pick the smaller item
ii
jj
KK
and move on
ii
jj
KK
Pick the smaller item
ii
jj
KK
and move on
The first half is now sorted
Quickly we do the other half
Divide...
and Divide...
and Divide until the lists can be no smaller
Merge pairs...
Merge quaters
Finally the second half is sorted
A final merge of both halves makes the list sorted
ii
jj
KK
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
ii
jj
KK
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
Pity you need extra space to ‘merge’ into otherwise
it would be pretty cool

Merge sort b

Editor's Notes

  • #3 Lets begin with a list to sort
  • #4 We split the array in two and look at the first half.
  • #5 We split that in two and look at the first half.
  • #6 We split that in two and look at the first half.
  • #7 We split that in two and look at the first half. We reach a single item so it must be sorted.
  • #8 We split that in two and look at the first half. We reach a single item so it must be sorted.
  • #9 We look at the other half. Its also a single array so it’s also sorted.
  • #10 We now have two sorted arrays which we need to merge . Using two pointers we move down them picking the smallest one each time. The final purple k pointer shows where the item will go.
  • #12 When both pointer reach the end of their respective lists we have all the items merged.
  • #13 So this sub list is sorted. We can move on to the other half.
  • #14 We sub divide again to get another single item. Its sorted.
  • #15 the other half of this pair is signle so its sorted.
  • #16 We now have to merge this pair.
  • #18 With these two eights done we can merge the first quarter.
  • #26 With the first quarter done we can move to the second quarter.
  • #56 With the first half done. I am going to speed up the process for the second half.
  • #58 We split
  • #59 And split
  • #60 And split until we have single items.
  • #61 then we merge halves together.
  • #62 and merge again.
  • #63 and again, until we are ready to merge with first half.
  • #64 Once the final merge is complete we are done.