Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 109283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:02:42+00:00 2026-05-11T02:02:42+00:00

The median of five is sometimes used as an exercise in algorithm design and

  • 0

The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons.

What is the best way to implement this ‘median of five using 6 comparisons’ in C# ? All of my attempts seem to result in awkward code 🙁 I need nice and readable code while still using only 6 comparisons.

public double medianOfFive(double a, double b, double c, double d, double e){     //     // return median     //     return c; } 

Note: I think I should provide the ‘algorithm’ here too:

I found myself not able to explain the algorithm clearly as Azereal did in his forum post. So I will reference his post here. From http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1061827085

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.

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

  2. Compare the two lower ones of each pair and eliminate the lowest one from the possibilities (3 comparisons)

  3. Add in the 5th number set aside to the number without a pair and compare the two (4 comparisons)

  4. Compare the two lowest of the two new pairs and eliminate the lower one (5 comparisons)

  5. Compare the one by itself and the lower of the last pair and the lower number is the median

    The possible median is within the parentesis

(54321)

5:4 3:2 2 comparisons

(4<5 2<3 1)

4:2 3 comparisons

2(4<5 3 1)

1:3 4 comparisons

2(4<5 1<3)

4:1 5 comparisons

1,2(4<5 3)

4:3 6 comparisons

1,2(3)4,5

Three is the median

Here is the C++ code I wrote to find median of five. Don’t mind its awkwardness:

double StageGenerator::MedianOfFive(double n1, double n2, double n3, double n4, double n5){     double *a = &n1, *b = &n2, *c = &n3, *d = &n4, *e = &n5;     double *tmp;      // makes a < b and b < d     if(*b < *a){         tmp = a; a = b; b = tmp;     }      if(*d < *c){         tmp = c; c = d; d = tmp;     }      // eleminate the lowest     if(*c < *a){         tmp = b; b = d; d = tmp;          c = a;     }      // gets e in     a = e;      // makes a < b and b < d     if(*b < *a){         tmp = a; a = b; b = tmp;     }      // eliminate another lowest     // remaing: a,b,d     if(*a < *c){         tmp = b; b = d; d = tmp;          a = c;     }      if(*d < *a)         return *d;     else         return *a;  }  

It should be more compact, isn’t it ?


As @pablito pointed out in his answer, the built-in List.Sort() cannot fulfill this requirement since it uses up to 13 comparisons :]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:02:43+00:00Added an answer on May 11, 2026 at 2:02 am

    This is basically just factoring out the swapping and sorting code from your C++ example:

    private static void Swap(ref double a, ref double b) {     double t = a;     a = b;     b = t; }  private static void Sort(ref double a, ref double b) {     if (a > b) {         double t = a;         a = b;         b = t;     } }  private static double MedianOfFive(double a, double b, double c, double d, double e){     // makes a < b and c < d     Sort(ref a, ref b);     Sort(ref c, ref d);      // eleminate the lowest     if (c < a) {         Swap(ref b, ref d);         c = a;     }      // gets e in     a = e;      // makes a < b     Sort(ref a, ref b);      // eliminate another lowest     // remaing: a,b,d     if (a < c) {         Swap(ref b, ref d);         a = c;     }      return Math.Min(d, a); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Not directly pv3d related but, could help ... It sounds… May 11, 2026 at 11:17 am
  • added an answer window.location.hash May 11, 2026 at 11:17 am
  • added an answer You should change the typedef to typedef BOOL (WINAPI *LPQueryFullProcessImageName)(… May 11, 2026 at 11:17 am

Related Questions

The median of five is sometimes used as an exercise in algorithm design and
A couple of years ago the media was rife with all sorts of articles
Sorry the title isn't more help. I have a database of media-file URLs that
How can I get the BSSID / MAC (Media Access Control) address of the
I'm trying to implement the Media Player custom field control described in this MSDN
I working on a distributed mediaplayer that uses the windows media player component. Now
What is the most appropriate media type (formally MIME type) to use when sending
So I want to trigger an event (pausing/unpausing some media) whenever the user presses
We are hosting the swf assets on a media hosting server and can't get
I embedded a default media player into my webpage with the following html <object

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.