How to sort three variables using at most two swaps?

后端 未结 10 1535
终归单人心
终归单人心 2020-12-10 00:56

The following algorithm can sort three variables x, y and z of type K which are comparable using operator<

10条回答
  •  孤街浪徒
    2020-12-10 01:42

    Cool question :)

    If assembly is available to you, and the values fit in a register, then you can probably do it extremely fast by just loading them into registers and doing a few compares, jumping to the right scenario to put the values back. Maybe your compiler makes this optimization already.

    Either way, if performance is your goal, take a look at the generated machine code and optimize there. For such a small algorithm that's where you can squeeze performance out of.

提交回复
热议问题