Original Post
Hello all. I have a (rather advanced, I think) programming question. Assume an array of length n, filled with real numbers. It is easy to write an algorithm to grab the smallest/largest of these numbers with n-1 comparisons. However, CLRS poses an exercise (9.1-1) in which you have to find the second smallest of n elements, with n + ceil(log2(n)) - 2 comparisons in the worst case. The given hint is to also find the smallest element. I have no idea how to do this... Where does the log come from, for example? Every algorithm I can devise takes about 2n comparisons in worst case. Note, you're not doing homework for me here - not in the strict sense of the word. It is not something I *need* to know/work out/blah, I just encountered it in the book and wondered how it's done, while studying for my exam. So, yes, it's kind of schoolwork, but more on a want-to-know basis, I don't think they are ever going to ask this. Anyone has an idea? Thank you very much! -Amarth