Sign up ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

I was just thinking today that the best approach to find two smallest numbers in any array would be to first sort(ascending order) it with some efficient algorithm like Quicksort(Average case complexity: nlogn) and then access the first two members of the array in O(1).

Is my approach optimal or there are some alternative better approaches?

share|cite|improve this question
    
Best regarding what? Given an environment that provides an O(nlogn) time sort, but no competent quickselect, I'd go for sort. –  greybeard yesterday
    
Why in the world would you need to sort an array to find smallest numbers? This is trivially done with a single pass. –  Davor 11 hours ago

4 Answers 4

If you keep track of the 2 smallest elements you have seen so far as you traverse the array, then you only need to go through the array once, and for each element you compare to the larger of the 2 current smallest and if the new element is smaller, then you replace the larger of the 2 current smallest with the new element. This requires only a few operations per element in the array as you traverse the array, so it's $O(n)$ with a very small constant. It's probably impossible to come up with a sort-based approach that ever runs faster, except maybe for an array of size 4 or 5 or less.

share|cite|improve this answer

No, it's not optimal. Do you know an efficient way of finding the smallest number in an array? Knowing the smallest number, could you adapt that method to find the second smallest?

share|cite|improve this answer

Hoare's algorithm, which Wikipedia calls Quickselect, can find the $k$ smallest elements of an array in $O(n)$ time for any fixed $k$.

It is a modified Quicksort algorithm that sorts the array but stops early, leaving the beginning part correct (in this case the first two elements) and the rest of the array in whatever partly-sorted state is most convenient.

share|cite|improve this answer
3  
For "find the two smallest" this is surely way overkill. –  vonbrand yesterday
    
I agree, for "find the two smallest" it is overkill. –  Mark Dominus yesterday
    
If the question is to find the two numbers at the median then this would be perfect. –  Derek 朕會功夫 18 hours ago
    
Not really, because in that case Hoare's algorithm is $O( N \log N)$, better than quicksort only by a constant factor. –  Mark Dominus 13 hours ago

The optimal number of comparisons (not necessarily the fastest one) goes like this for $n = 2^k$:

  1. Compare $a_1$ and $a_2$, $a_3$ and $a_4$, and so on. Store only the smallest of each pair in a list $b_1,\ldots,b_{n/2}$.

  2. Repeat $k-1$ more times to get the minimum $a_{\min}$.

  3. Let $L$ be the set of all $k$ elements which were compared to $a_{\min}$ and were larger. Find the minimum of $L$. This is the second minimal element.

This algorithm uses $n + \log_2 n - 2$ comparisons, which is the optimal number of comparisons. However, implementing the logic isn't trivial, so in practice it might be slower than the algorithm in user2566092's answer.

share|cite|improve this answer
    
At step 2 you have used $n-1$ compares. The smallest element has been compared to $k$ others, so we need $k-1$ compares. The total is then $n+k-2$, I believe. –  Ross Millikan yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.