algorithm - Code to calculate "median of five" in C# -


Note: Please do not interpret as "homework questions". This is the only thing I am eager to know :)

The average of five is sometimes used as an exercise in the algorithm design and it only <6> to use only 6 comparisons Is known for . / P>

What's the best way to use this "6 Comparison with average of 5" in C # ? All of my attempts appear in strange code :( I still need good and readable code using only 6 comparisons.

  Public Double Intermediate (Double A, Double B, Double C, Double D, Double E) // // Return Mean / Return C;}  

Note: I think I'm here "algorithm "Should also be given:

I could not clearly explain the algorithm like Azerea L in his forum post, so I will refer to his post here.

Well I had rejected this problem in one of my work and I helped this forum I had finally changed but there was no help. I finally came to know how to do this.

  1. Start a mergen with the first 4 elements and each pair with the order (2 comparison )

  2. Compare the two lower parts of each pair and End the lowest one by NOW (compare 3)

  3. Add a different set number 5 without a pair with the number Compare the ND two (4 comparison)

  4. Compare two of the two pairs of two minimum and end the lower one (5 comparison)

  5. 4: 2 3 comparison

    p >

    2 (4 & lt; 5 3 1)

    1: 3 4 Comparison

    2 (4 <5 1 <3)

    4: 1 5 Comparison

    1,2 (4 <5 p)

    4: 3 6 comparison

    1,2 (3) 4,5

    Edit: To stop yourself from receiving more downwaves as your request, this is the C ++ code I wrote to search in midsection. Do not mood:

      Double Stage Generator :: Medianoffice (Double N1, Double N2, Double N3, Double N4, Double N5) {Double * A = and N1, * B = & Amp; N2, * c = & amp; N3; * D = & amp; N4; * E = & amp; N5; Double * tmp; // makes & 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; } // at least eleminate if (* c & lt; * a) {tmp = b; B = D; D = TMP; C = A; } // E in A = E; // makes & lt; B and B & LT; D if (* b & lt; * a) {tmp = a; A = B; B = TMP; } // End one more // remaing: a, b, d if (* a & lt; * c) {tmp = b; B = D; D = TMP; A = C; } If (* D & L; * A) returns * D; And return * A; }  

    It should be more compact, is not it?

    Edit:

    As @Publito pointed out the underlying list. Source () out of its reply can not meet this requirement because it is 13 comparison Uses to:]

This is basically swapping and sorting from your C ++ example Factoring code:

  Private static zero swap (Ref Double A, Ref Double B) {Double T = A; A = B; B = T; } Sort Private Static Zero (Ref Double A, Ref Double B) {if (a & gt; b) {double t = a; A = B; B = T; }} Private Static Double Mediofofoffe (Double A, Double B, Double C, Double D, Double E) {// A & lt; B and C & LT; D-ordered (referee A, referee B); Sort (Ref C, Ref D); // If at least eleminate (c & lt; a) {swap (ref B, referee d); C = A; } // E in A = E; // makes & lt; B. Sort (referee A, referee B); // Another low // remaing finish: A, B, D if (a & lt; c) {swap (ref B, referee D); A = C; } Return mathematics .min (D, A); }  

Comments