Merge original( a)2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
8. 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
9. 2 5 7 1 3 8
1 2 3 5
Sorted list A Sorted list B
Merged List
Merge sort secret sauce
10. 2 5 7 1 3 8
1 2 3 5 7
Sorted list A Sorted list B
Merged List
Merge sort secret sauce
11. 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
12. 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
31. 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))
32. 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
Editor's Notes Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort Lets begin with a list to sort