title: How do I calculate the "median of five" in C#?
body: <p>The median of five is sometimes used as an exercise in algorithm design and is known to be computable <strong>using only 6 comparisons</strong>.</p>
  
  <p>What is the best way to implement this <strong>"median of five using 6 comparisons"</strong> in C# ? All of my attempts seem to result in awkward code :( I need nice and readable code while still using only 6 comparisons.</p>
  
  <pre><code>public double medianOfFive(double a, double b, double c, double d, double e){
      //
      // return median
      //
      return c;
  }
  </code></pre>
  
  <p><strong>Note:</strong> I think I should provide the "algorithm" here too:</p>
  
  <p>I found myself not able to explain the algorithm clearly as <em>Azereal</em> did in his forum post. So I will reference his post here. From <a href="http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1061827085" rel="nofollow noreferrer">http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1061827085</a></p>
  
  <blockquote>
    <p>Well I was posed this problem in one
    of my assignments and I turned to this
    forum for help but no help was here. 
    I eventually found out how to do it.</p>
    
    <ol>
    <li><p>Start a mergesort with the first 4 elements and order each pair (2
    comparisons)</p></li>
    <li><p>Compare the two lower ones of each pair and eliminate the lowest one from
    the possibilities (3 comparisons)</p></li>
    <li><p>Add in the 5th number set aside to the number without a pair and compare
    the two (4 comparisons)</p></li>
    <li><p>Compare the two lowest of the two new pairs and eliminate the lower one
    (5 comparisons)</p></li>
    <li><p>Compare the one by itself and the lower of the last pair and the lower
    number is the median</p>
    
    <p>The possible median is within the
    parentesis</p></li>
    </ol>
    
    <p>(54321)</p>
    
    <p>5:4   3:2    2 comparisons</p>
    
    <p>(4&lt;5 2&lt;3  1)  </p>
    
    <p>4:2      3 comparisons</p>
    
    <p>2(4&lt;5 3 1)</p>
    
    <p>1:3      4 comparisons</p>
    
    <p>2(4&lt;5 1&lt;3)</p>
    
    <p>4:1      5 comparisons</p>
    
    <p>1,2(4&lt;5 3)</p>
    
    <p>4:3      6 comparisons</p>
    
    <p>1,2(3)4,5  </p>
    
    <p>Three is the median</p>
  </blockquote>
  
  <p>Here is the C++ code I wrote to find median of five. Don't mind its awkwardness:</p>
  
  <pre><code>double StageGenerator::MedianOfFive(double n1, double n2, double n3, double n4, double n5){
      double *a = &amp;n1, *b = &amp;n2, *c = &amp;n3, *d = &amp;n4, *e = &amp;n5;
      double *tmp;
  
      // makes a &lt; b and b &lt; d
      if(*b &lt; *a){
          tmp = a; a = b; b = tmp;
      }
  
      if(*d &lt; *c){
          tmp = c; c = d; d = tmp;
      }
  
      // eleminate the lowest
      if(*c &lt; *a){
          tmp = b; b = d; d = tmp; 
          c = a;
      }
  
      // gets e in
      a = e;
  
      // makes a &lt; b and b &lt; d
      if(*b &lt; *a){
          tmp = a; a = b; b = tmp;
      }
  
      // eliminate another lowest
      // remaing: a,b,d
      if(*a &lt; *c){
          tmp = b; b = d; d = tmp; 
          a = c;
      }
  
      if(*d &lt; *a)
          return *d;
      else
          return *a;
  
  } 
  </code></pre>
  
  <p>It should be more compact, isn't it ?</p>
  
  <hr>
  
  <p>As @pablito pointed out in his answer, the built-in <code>List.Sort()</code> cannot fulfill this requirement since it uses up to 13 comparisons :]</p>