您的位置:首页 > 理论基础 > 数据结构算法

求三个数的最大最小值

2015-10-28 22:53 453 查看

【0】README

0.1) google 搜索出来的答案,真的很让我伤心, 全是大粪啊, 你们进行了多少次的比较, 应该是6次吧,我也是醉完了, 写个非大粪的version (我这里的比较只有两次)

【2】源代码如下(注意数据结构):

// get the index storing the maximum among elements under left, parent and right
int maxIndex(int left, int parent, int right, BinaryHeap bh)
{
int maxIndex;

maxIndex = left;
if(bh->elements[parent] > bh->elements[maxIndex])
maxIndex = parent;
else if(bh->elements[right] > bh->elements[maxIndex])
maxIndex = right;

return maxIndex;
}

// get the index storing the minimum among elements under left, parent and right
int minIndex(int left, int parent, int right, BinaryHeap bh)
{
int minIndex;

minIndex = left;
if(bh->elements[parent] < bh->elements[minIndex])
minIndex = parent;
else if(bh->elements[right] < bh->elements[minIndex])
minIndex = right;

return minIndex;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息