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

  • SEARCH
  • Home
  • 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 8110575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:48:07+00:00 2026-06-06T01:48:07+00:00

Given five random elements, it’s possible to find the median using just six comparisons.

  • 0

Given five random elements, it’s possible to find the median using just six comparisons. But I have an extra requirement in that the following condition is also satisfied:

a < c && b < c && c < d && c < e

It is NOT required for a < b or d < e.

I’ve been able to satisfy this condition using seven comparisons, but not six. This is my code, which should be self explanatory:

void medianSort(int[] arr)
{
    assert(arr.length == 5);

    void sortPair(ref int a, ref int b)
    {
        if(a > b) swap(a, b);
    }

    sortPair(arr[0], arr[1]);
    sortPair(arr[3], arr[4]);
    sortPair(arr[0], arr[3]);
    sortPair(arr[1], arr[4]);
    sortPair(arr[2], arr[3]);
    sortPair(arr[1], arr[2]);
    sortPair(arr[2], arr[3]);
}

I have a quick sort that I wish to enhance by using a median of five to choose the pivot. By satisfying that condition, five elements are already sorted.

Is it possible to accomplish this in six comparisons, or is seven the best I can do?

  • 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. Editorial Team
    Editorial Team
    2026-06-06T01:48:09+00:00Added an answer on June 6, 2026 at 1:48 am

    I grinded away at the problem for two hours, but I found a solution. The important thing is to keep track of the comparisons made, which I’ve done in great detail with the comments. The function is written in D.

    void medianSort(alias pred = "a < b", T)(ref T a, ref T b, ref T c, ref T d, ref T e)
    {
        alias binaryFun!pred less;
        bool greater(T a, T b){ return !less(a, b); }
    
        if(greater(a, b)) swap(a, b); // #1
        // a<b
        if(greater(d, e)) swap(d, e); // #2
        // a<b d<e
    
        if(less(a, d)) // #3
        {
            // a<d<e a<b
            // eliminate a
            // d<e
            if(greater(b,c)) swap(b,c); // #4
            // b<c d<e
        }
        else // if(less(d, a))
        {
            // a<b d<a d<e
            // d<a<b   d<e
            swap(a, d);
            // a<d<b   a<e
            // eliminate a
            // d<b
            swap(d, b);
            // b<d
            if(greater(c, e)) swap(c, e); // #4
            // b<d c<e
            swap(c, d);
            // b<c d<e
        }
    
        // b<c d<e
        if(less(b, d)) // #5
        {
            // b<c b<d d<e
            // b<c b<d<e
            // eliminate b
            // d<e
        }
        else
        {
            // b<c d<e d<b
            // d<b<c d<e
            swap(b, d);
            // b<d<c b<e
            // eliminate b
            // d<c
            swap(c, e);
            // d<e
        }
    
        // d<e
        // median is smaller of c and d
        if(greater(c, d)) swap(c, d); // #6
    }
    

    Python: http://pastebin.com/0kxjxFQX

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have five values, A, B, C, D and E. Given the constraint A
Just using DataMapper for the first time. I have set up a table in
Imagine you have a set of five elements (A-E) with some numeric values of
I have been given an exercise to solve the zebra puzzle using a constraint
Given a package, how can I automatically find all its sub-packages?
I searched around a bit, but could not find anything useful. Could someone help
According to Five Simple Database Design Errors You Should Avoid by Anith Sen, using
We've been given a stored procedure from our RPG folks that returns six data
We have started learning Java in school, and we have been given a few
This is a kind of newbie question, but I couldn't find a solution. I

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.