排序三个整数

可以用if...else的判断分支去列举所有可能的结果,但n个数就需要n!个判断,从代码量上来说是不可接受的。(写法略)

那如果每次操作找出当前所有数中的最值(最大或最小),然后就可以不再管那个最值了,从而缩小下一次所要处理的数据量,直至处理完所有数据。

// exch means exchange the two values
if
(a > b) exch(a, b); if (a > c) exch(a, c); // now a is the smallest one if (b > c) exch(b, c); // sorted

想起来这和选择排序如出一辙啊

猜你喜欢

转载自www.cnblogs.com/giantRan/p/11111000.html