0

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.

Community
  • 1
  • 1
deryk
  • 131
  • 2
  • 7

6 Answers6

3
int smallest, secondsmallest;
if (array[0] < array[1]) {
    smallest = array[0];
    secondsmallest = array[1];
} else {
    smallest = array[1];
    secondsmallest = array[0];
}
for(int i = 2; i < array_size; i++) {
    if (array[i] < smallest) {
        secondsmallest = smallest;
        smallest = array[i];
    } else if (array[i] < secondsmallest) {
        secondsmallest = array[i];
    }
}
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Your method is O(n^2). Read this: http://stackoverflow.com/questions/3715935/algorithm-find-index-of-2nd-smallest-element-from-an-unknown-array – deryk Jun 08 '11 at 02:23
  • 1
    @deryk: I quite clearly only loop through the array once. That makes it kind of impossible for my algorithm to be O(n^2). – Puppy Jun 08 '11 at 02:28
  • Sorry, your algorithm is 2N > n+lg(n)-2 – deryk Jun 08 '11 at 04:18
  • 2
    @deryk: You should brush up on complexity analysis. O(n) is the same as O(n + lgn -2), because you are only concerned with asymtotic behavior. – Dennis Zickefoose Jun 08 '11 at 05:05
1

Odd that no one has bothered to give this its name. Including the original questioner. For those unfamiliar with it, it is called the i th order statistic, and is discussed in many fine algorithm books. It is indeed O(n) (actually theta of n) for n elements.

Don Wakefield
  • 8,693
  • 3
  • 36
  • 54
0

Try this, should work

min1 = a[0]; 
min2 = 0;
int i = 0;
for (i = 0; i < a.length; i++){
   if (min1 < a[i]){
      min2 = min1;
      min1 = a[i];
   }
}

a.length is a Java instance of array. I'm not sure if C++ has the same one.

sabbibJAVA
  • 1,068
  • 1
  • 11
  • 17
0
// A simple implementation with O(n) runtime complexity;
int findSecondMin(vector<int>& nums) {
    if (nums.size() < 2) throw runtime_error("no second minimum !");
    int min1 = nums[0], min2 = INT_MAX;
    for(int i=1;i<nums.size();i++) {
      if (nums[i] < min1) {
        min2 = min1;
        min1 = nums[i];
      } else if (nums[i] < min2) {
        min2 = nums[i];
      }
    }
    return min2;
}
Jeremy Huang
  • 211
  • 6
  • 13
0

You should keep the running "candidates" (and maybe number of their occurrences, depending on your strict definition of "2nd") for the smallest and second smallest value all along the way.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 1
    I can't find it in my heart to -1 this but we really shouldn't do this for homework problems... – Andrei Jun 07 '11 at 22:01
  • @Andrei: I assume my hint is not too strong, why? – Vlad Jun 07 '11 at 22:02
  • It is not actually a homework. 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. – deryk Jun 08 '11 at 01:47
0

Well, you kind of answered your own question:

  1. How do you keep track of the smallest element? You store an index to it.
  2. How do you find the second smallest element? Search again and ignore the element you found first.
MSN
  • 53,214
  • 7
  • 75
  • 105
  • That is not the best solution. The best one is to find the smallest element first and then find the 2nd smallest one by comparing competitors of the found smallest element. – deryk Jun 08 '11 at 01:45
  • @deryk, if you are asking for the "optimal" solution, you could probably do worse than looking at the implementation of `nth_element(...)` in the C++ STL. That is O(n), assuming you can mutate the array in place. – MSN Jun 08 '11 at 22:30