Neighbours are not enough
transcript
How out of order is a playlist? Add up the gaps of out of order pairs. A shortcut looks tempting.
Sum only neighbouring pairs. Three and two add one. Two and one add one. Three and one are far apart. That pair adds two more.
The shortcut says two. The true answer is four. So the shortcut is wrong.
A score can be out of order with any later score. Not only the one next to it. A long list has millions of such pairs.
A stress test against all pairs finds it at once. On the first random list, the shortcut gives nineteen. The pairs give forty-six.
Use a merge sort. Carry the sum of the waiting left values. Each right value adds many gaps at once.
Name what the shortcut ignores. Search for a tiny counterexample by hand. Then run a slow twin on random lists.
A hundred thousand scores have five billion pairs. That is why the merge carries a sum.
Neighbours miss far pairs. Test against all pairs. Full breakdown in the lecture.