Keep three values, not the list
transcript
You have a list of a hundred thousand values. You need only the three highest. Do you sort all of them? A full sort works. It does more than needed.
A hundred thousand values make five billion pairs. A judge runs a hundred million steps a second. Comparing every pair takes close to a minute. That is the cost of looking at everything with everything.
Ask what can change the answer. Only the three highest values can. Everything else is noise. So do not sort everything, and do not compare every pair.
Keep the two top places, and read once. The nine pushes the four down. The second nine takes second place. Equal values stay separate. Three variables, updated once per value, answer the question.
Keep three variables in order. Read the list once, and let each value find its place. Start the places below every possible value.
Three variables beat a full sort. One pass is enough. Full breakdown in the lecture.