最低编号比较以找到3个数字的中位数 [英] Minimum no. of comparisons to find median of 3 numbers

查看:85
本文介绍了最低编号比较以找到3个数字的中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行quicksort,我希望将枢轴设置为中位数或三位数.这三个数字是第一个元素,中间元素和最后一个元素.

I was implementing quicksort and I wished to set the pivot to be the median or three numbers. The three numbers being the first element, the middle element, and the last element.

我能否以更少的数字找到中位数?比较?

Could I possibly find the median in less no. of comparisons?

median(int a[], int p, int r)
{
    int m = (p+r)/2;
    if(a[p] < a[m])
    {
        if(a[p] >= a[r])
            return a[p];
        else if(a[m] < a[r])
            return a[m];
    }
    else
    {
        if(a[p] < a[r])
            return a[p];
        else if(a[m] >= a[r])
            return a[m];
    }
    return a[r];
}

推荐答案

您不能一次完成,而只能使用两个或三个,所以我想说您的比较数最少已经.

You can't do it in one, and you're only using two or three, so I'd say you've got the minimum number of comparisons already.

这篇关于最低编号比较以找到3个数字的中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆