This answer describes how to find the second greatest element; the search for the second smallest can be done in a similar way. For simplicity, we also assume that all numbers are distinct.
To find the greatest element, let's build a championship tree: connect the elements, decide which is greater (the one who is the winner), then combine the winners, decide which is greater, and so on, until you find the “champion” who is the biggest an element. It takes n steps. Now the second greatest element must be compared with the champion. (because only the champion could defeat him). log n elements were compared with the champion, so choose the largest of them; it takes log n steps.
As an example, let's see how this works for a set of numbers [6,4,3,5,2,1]. In the first round of the pair (6.4), (3.5), (2.1). Winners are big elements in each pair, i.e. 6.5.2. In the second round of the pair (6.5), 2. (2 does not have a pair here, so it automatically rises to the next round). The winners of the second round are 6 and 2, in the third round there is a single pair (6.2), 6 is the winner. Now, combining the elements and choosing the winner, we created a tree with a root, binary: 
This tree has the property that for node x and its children y,z we have x>=y, x>=z , so we know that the largest element belongs to the vertex (in the root). We also know that the second greatest element of w did not hit the vertex, so it has a parent element in the tree. But its parent element is greater than or equal to w , therefore, at some level of the tree, one of the children of the largest element w . (In other words, the second greatest element can be “defeated” by the largest element). So, all we need to do is return to the path that the greatest element has taken, and collect all the direct children, we know that among them there is the second largest. In our case, these are elements 2,5,4. (In general, of these, about log n , where log denotes the logarithm of the base two, because the tree has a value of log n high.). Of these elements, we select the largest with any method that takes log n steps, and we find the second largest.
All this may remind us of the championship, where the numbers indicate how “good” each team is, therefore, the term “championship tree”.