title: Recursive Way to Find 2nd/3rd Smallest Element In Array
body: <p>Below is a code I written to find the smallest element of an unsorted array in C++.</p>
  
  <p>How should I go about editing my recursive function to find the second or even the third smallest element in the array? </p>
  
  <p>I understand there are other methods out there (I've searched), however I'd like to know how to do it using a recursive function like this which works for just the minimum number.</p>
  
  <pre><code>int min(vector&lt;int&gt; &amp;array, int &amp;min, int &amp;next_min, int left,int right)
  {    
      int a;        
      int b;
  
      if(left == right) { 
          return array[left];
      } else {   
          int mid= (right+left)/2;
  
          a = min(array,left,mid);
          b = min(array,mid+1,right);
  
          if (a&lt;b) 
              return b;
          else         
              return a;
      }
  }
  </code></pre>
  
  <p>Many thanks in advance</p>
  
  <p>Here is my attempt at finding the 2nd smallest: </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  int counter=0;
  
  void minf(vector&lt;int&gt; &amp;array,int left,int right,int &amp;min, int &amp;next_min);
  
  int main()
  {
      int next_min;
  
      cout &lt;&lt; "Enter integers one line at a time. (Enter -999 to terminate)" &lt;&lt; endl;
  
      vector&lt;int&gt; array;  // 
      int input;
  
      while(true){
          cin &gt;&gt; input;
          if(input == -999) //check for termination condition
          break;
          array.push_back(input);
      }
  
      int imin;
      int imax;
  
      cout&lt;&lt;"Enter imin, then imax"&lt;&lt;endl;
      cin&gt;&gt;imin;
      cin&gt;&gt;imax; 
  
      cout&lt;&lt;"Min number is " &lt;&lt; next_min &lt;&lt;endl;
  
      cin.get();
      system("pause");
      return 0;
  }
  
  void minf(vector&lt;int&gt; &amp;array,int left,int right,int &amp;min, int &amp;next_min)
  {
      int a;
      int b;
      int c;
      int d;
  
      if(left == right) { 
          min = array[left];
          next_min = 2147483647;
      } else {
          int mid= (right+left)/2;
  
          minf(array,left,mid,a,b);
          minf(array,mid+1,right,c,d);
  
          if (a &lt; b &amp;&amp; a &lt; c &amp;&amp; a &lt; d) {
              min = a;
              if (b&lt;c &amp;&amp; b &lt;d)
                  next_min = b;
              else if (c &lt; b &amp;&amp; c &lt; d)
                  next_min = c;
              else
                  next_min = d;
          }    
  
          if (b &lt; a &amp;&amp; b &lt; c &amp;&amp; b &lt; d){
              min = b;
              if (a&lt;c &amp;&amp; a &lt;d)
                  next_min = a;
              else if (c &lt; b &amp;&amp; c &lt; d)
                  next_min = c;
              else
                  next_min = d;
          }
  
          if (c &lt; a &amp;&amp; c &lt; b &amp;&amp; c &lt; d) {
              min = c;
              if (b&lt;a &amp;&amp; b&lt;d)
                  next_min = b;
              else if (a &lt; b &amp;&amp; a &lt; d)
                  next_min = a;
              else
                  next_min = d;
          }   
  
          if (d &lt; a &amp;&amp; d &lt; c &amp;&amp; d &lt; b){
              min = d;
              if (a&lt;c &amp;&amp; a &lt;b)
                  next_min = a;
              else if (c &lt; b &amp;&amp; c &lt; a)
                  next_min = c;
              else
                  next_min = b;
          }     
      }
  }
  </code></pre>