Order and balance
transcript
Values keep arriving. You need the median each time. Sorting again is slow. Two heaps do it faster.
Fact one: every lower value is at most every upper value. Fact two: the sizes differ by at most one. The lower half may hold one more. Then the tops are the middle.
Add each value to the lower half. Move the lower top across. Move one back if the sizes need it. The medians are five, three, five. Then the medians are three, five, four.
An odd count has one middle. Read the lower top. An even count has two middles. Average both tops. Round down.
The lower top is the largest of the low values. The upper top is the smallest of the high values. So together they sit at the middle. Each step then costs about log n.
Keep the halves in order. Keep the sizes balanced. Full breakdown in the lecture.