Tallest first, then insert at the count
transcript
Friends lined up for a group photo. The order was lost. Each person knows two numbers. Height, and how many taller or equal are in front.
Sort the people tallest first. On equal heights, the smaller count goes first. Then insert each person at the position equal to their count.
The sorted heights: seven, seven, six, five, five, four. The sevens go in first. Their counts are zero and one. Six has count one. It goes between the sevens. The fives and the four follow. The line is five, seven, five, six, four, seven.
A shorter person is never counted by a taller one. Taller people ignore them. So earlier inserts stay correct. On equal heights, the smaller count goes first.
Three people have height five, and counts zero, one, two. Larger count first gives zero, two, one. Smaller count first gives zero, one, two.
Sort tallest first. Insert at the count. Full breakdown in the lecture.