Coding & data structures · Sorting and selection — order, and the parts of it you actually need

Merging, and what divide-and-conquer actually costs

Merge sort is worth learning for one reason that has nothing to do with sorting — the merge step is a place to compute things, and once you see that, counting inversions, merging k sorted streams, and the whole "split, solve, combine" family stop being separate tricks and become one recurrence you can read off the code.

26 min read Full lesson in the app Patterns: merge two sorted, fill from the back, recursion tree, counting during combine, k-way merge

What you'll be able to do

Before this: complexity-honestly, sorting-and-comparators

The rest of this lesson is in the app

Merge sort is worth learning for one reason that has nothing to do with sorting — the merge step is a place to compute things, and once you see that, counting inversions, merging k sorted streams, and the whole "split, solve, combine" family stop being separate tricks and become one recurrence you can read off the code. This walkthrough runs about 26 minutes, with runnable code you can edit and re-run as you read.

Continue in ChannelPulse

The first module of every track is free to read on the web — see what's open in Coding & data structures.