用很少的比较对4个数字进行排序 [英] Sort 4 number with few comparisons

查看:94
本文介绍了用很少的比较对4个数字进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在5个比较中对4个数字进行排序?

How can I sort 4 numbers in 5 comparisons?

推荐答案

取数字{a,b,c,d},分为两组{a,b} {c,d}。
分别订购这两个集合,则得到(e,f)(g,h)。那是每组一个比较。

Takes numbers {a,b,c,d}, split into 2 sets {a,b} {c,d}. Order each of those 2 sets, so you get (e,f) (g,h). That's one comparison per set.

现在从前面选择最低的(比较e,g)。现在是三个比较。
从(e,h)或(f,g)中选择下一个最低。四点。
比较最后两个元素(如果两个元素来自同一集合,因此已经排序,您甚至可能不需要此步骤)。那就是五个。

Now pick lowest from the front (compare e,g). That's now three comparisons. Pick next lowest from either (e, h) or (f, g). That's four. Compare the last two elements (you might not even need this step if the two elements are from the same set, and thus already sorted). So that's five.

这篇关于用很少的比较对4个数字进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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