Merge sort
A sorting algorithm that splits the list into halves, sorts them and merges them: O(n log n).
Example
Sorting 1 million items with merge sort takes about 20 million steps.
Test yourself: Merge sort
Merge sort
Merge sort stops splitting when pieces have ___ element.
A single element is already sorted. With 16 elements, 16 pieces of 1 are reached in 4 levels of splitting.
Merge sort
Merging 2 sorted lists of 500 elements each costs about ___ steps.
Each of the 1,000 elements is copied to the result once: O(n).
Merge sort
Merge sort with 1,024 elements has ___ merge levels.
logâ‚‚ 1,024 = 10. At each level all 1,024 are copied: about 10,240 steps in total.
0/3
â–¶ PlayLessons that use it
What is an algorithm
You already use algorithms
Measuring efficiency
Fast and light on memory
Time complexity
Count steps, not seconds
Space complexity
How much memory it asks for
Arrays
Adjacent cells
What makes a good sort
5 things you ask of it
Insertion sort
Like arranging the cards in your hand
Quick sort
A pivot and 2 sides
Merge sort
Split in half and merge in order
Heap sort
Take the maximum and leave it at the end
Counting sort
Sorting by counting
Divide and conquer
Split, solve and combine
When DP works
3 properties
