Merge sort
Merge sort has a secret sauce - it’s how you mix lists
2 5 7 1 3 8
Sorted list A Sorted list B
Merged List
Merge sort
2 5 7 1 3 8
Sorted list A Sorted list B
Merged List
To merge two sorted lists begin at the beginning of each
Merge sort secret sauce
2 5 7 1 3 8
Sorted list A Sorted list B
Merged List
You have two markers (red)for for each list.
Merge sort secret sauce
2 5 7 1 3 8
Sorted list A Sorted list B
Merged List
The next item in the merged list is the lower of the two
Merge sort secret sauce
2 5 7 1 3 8
1
Sorted list A Sorted list B
Merged List
The next item in the merged list is the lower of the two
Merge sort secret sauce
2 5 7 1 3 8
1 2
Sorted list A Sorted list B
Merged List
If you use a marker you move it forward and try again
Merge sort secret sauce
2 5 7 1 3 8
1 2 3
Sorted list A Sorted list B
Merged List
The next item in the merged list is the lower of the two
Merge sort secret sauce
2 5 7 1 3 8
1 2 3 5
Sorted list A Sorted list B
Merged List
Merge sort secret sauce
2 5 7 1 3 8
1 2 3 5 7
Sorted list A Sorted list B
Merged List
Merge sort secret sauce
2 5 7 1 3 8
1 2 3 5 7 8
Sorted list A Sorted list B
Merged List
When one list is used you can just use up all of the other
Merge sort secret sauce
2 5 1 3 4 7 8
1 2 3 5 7 8
Sorted list A Sorted list B
Merged List
List don’t even have to be the same length to work!
Merge sort secret sauce
Once you know how
to merge two sorted
lists the rest is easy
Begin with unsorted list
Split into two
Split each list into two again
Split each list into two again, and again
Split each list into two again, and again
Until each list has one item in it
Each one item list is ‘sorted’
Now move back and look at the pairs
Merge the pairs as first described...
... to make sorted pairs
Now merge pairs lists keeping them sorted
Merge pairs of lists together...
Merge pairs of lists together...
Finally we get back to the first two lists...
Which we merge
Leaving the original list sorted
Your done!
2 5 1 3 8 7 4
1 2 3 5 7 8
Sorted list A Sorted list B
Merge sort secret sauce
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
2 5 1 3 4 7 8
1 2 3 4 5 7
Sorted list A Sorted list B
Merge sort secret sauce
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 neet

Merge original( a)

  • 1.
  • 2.
    Merge sort hasa secret sauce - it’s how you mix lists 2 5 7 1 3 8 Sorted list A Sorted list B Merged List Merge sort
  • 3.
    2 5 71 3 8 Sorted list A Sorted list B Merged List To merge two sorted lists begin at the beginning of each Merge sort secret sauce
  • 4.
    2 5 71 3 8 Sorted list A Sorted list B Merged List You have two markers (red)for for each list. Merge sort secret sauce
  • 5.
    2 5 71 3 8 Sorted list A Sorted list B Merged List The next item in the merged list is the lower of the two Merge sort secret sauce
  • 6.
    2 5 71 3 8 1 Sorted list A Sorted list B Merged List The next item in the merged list is the lower of the two Merge sort secret sauce
  • 7.
    2 5 71 3 8 1 2 Sorted list A Sorted list B Merged List If you use a marker you move it forward and try again Merge sort secret sauce
  • 8.
    2 5 71 3 8 1 2 3 Sorted list A Sorted list B Merged List The next item in the merged list is the lower of the two Merge sort secret sauce
  • 9.
    2 5 71 3 8 1 2 3 5 Sorted list A Sorted list B Merged List Merge sort secret sauce
  • 10.
    2 5 71 3 8 1 2 3 5 7 Sorted list A Sorted list B Merged List Merge sort secret sauce
  • 11.
    2 5 71 3 8 1 2 3 5 7 8 Sorted list A Sorted list B Merged List When one list is used you can just use up all of the other Merge sort secret sauce
  • 12.
    2 5 13 4 7 8 1 2 3 5 7 8 Sorted list A Sorted list B Merged List List don’t even have to be the same length to work! Merge sort secret sauce
  • 13.
    Once you knowhow to merge two sorted lists the rest is easy
  • 14.
  • 15.
  • 16.
    Split each listinto two again
  • 17.
    Split each listinto two again, and again
  • 18.
    Split each listinto two again, and again
  • 19.
    Until each listhas one item in it
  • 20.
    Each one itemlist is ‘sorted’
  • 21.
    Now move backand look at the pairs
  • 22.
    Merge the pairsas first described...
  • 23.
    ... to makesorted pairs
  • 24.
    Now merge pairslists keeping them sorted
  • 25.
    Merge pairs oflists together...
  • 26.
    Merge pairs oflists together...
  • 27.
    Finally we getback to the first two lists...
  • 28.
  • 29.
  • 30.
  • 31.
    2 5 13 8 7 4 1 2 3 5 7 8 Sorted list A Sorted list B Merge sort secret sauce Merging lists takes as long as the list (order(N)) You do this log(N) times Making algorithm Order (N log(N))
  • 32.
    2 5 13 4 7 8 1 2 3 4 5 7 Sorted list A Sorted list B Merge sort secret sauce 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 neet

Editor's Notes

  • #15 Lets begin with a list to sort
  • #16 Lets begin with a list to sort
  • #17 Lets begin with a list to sort
  • #18 Lets begin with a list to sort
  • #19 Lets begin with a list to sort
  • #20 Lets begin with a list to sort
  • #21 Lets begin with a list to sort
  • #22 Lets begin with a list to sort
  • #23 Lets begin with a list to sort
  • #24 Lets begin with a list to sort
  • #25 Lets begin with a list to sort
  • #26 Lets begin with a list to sort
  • #27 Lets begin with a list to sort
  • #28 Lets begin with a list to sort
  • #29 Lets begin with a list to sort
  • #30 Lets begin with a list to sort