Let me make things clear: I want to know more about the implementation of the "best" ALGORITHM how to find 2nd smallest element in array, which is described here:
Algorithm: Find index of 2nd smallest element from an unknown array
First, I am completely clear about the ALGORITHM how to find 2nd smallest element in array. The best solution is O(n+lg(n)-2) which can be achieved by first obtaining the smallest element S and then searching the smallest element of S's competitors. Read here: Algorithm: Find index of 2nd smallest element from an unknown array
What I cannot understand is how to implement it.
Especially, after finding the smallest element, how to track back those competitors of the smallest element such that I can find 2nd smallest element in those competitors?
NO NEED TO SORT. Quicksort is O(nlg(n)) which is worse than O(n+lg(n)-2).
There have been too many people talking about the best solution while nobody actually gave an implementation. Besides, I found the best solution is just theoretical best. It is very hard to implement it following that "best" solution.