This answer describes how to find the second greatest element; finding the second smallest can be done analogously. For simplicity, we also assume that all numbers are different.
In order to find the greatest element, let us build a 'championship' tree: pair up the elements, decide which is greater (that one is the 'winner') then pair up the winners, decide which is greater, and so on, until you find the 'champion', which is the greatest element. This takes n steps. Now, the second greatest element must have been compared to the champion. (because only the champion could defeat it). log n elements have been compared to the champion, so from these, pick the greatest; this takes log n steps.
As an example, let us see how this works for the number tuple [6,4,3,5,2,1] . In the first round, pairs are (6,4), (3,5), (2,1). Winners are the greater elements in each pair, that is, 6,5,2. In the second round pairs are (6,5), 2. (2 has no pair here so it will get promoted to the next round automatically). Winners of the second round are 6 and 2, in the third round the only pair is (6,2), 6 is the winner. Now, by pairing up elements and choosing a winner we have built up a (rooted, binary) tree:
This tree has the property that for a node x
and its children y,z
we have x>=y, x>=z
, so
we know that the greatest element is the one at the top (at the root). We also know that the second greatest element w
did not make it to the top, so it has a parent in the tree. But its parent is greater than or equal to w
, so at some level of the tree, one of the children of the greatest element is w
. (In other words, the second greatest element could only be 'defeated' by the greatest element). So all we have to do is go back on the path the greatest element has taken and collect all direct children, we know the second largest is among them. In our case, these are the elements 2,5,4 . (In general, there are about log n
of them, where log
denotes base two logarithm, because the tree is about log n
high.). From these elements we pick the largest with any method that takes log n
steps, and we found the second largest.
All this may remind us to a championship, where numbers denote how 'good' each team is, hence the term 'championship tree'.