Idempotent partition around median of 5?

Era Scarecrow via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 4 12:57:01 PST 2016


On Thursday, 4 February 2016 at 20:30:57 UTC, Timon Gehr wrote:
> At most 6 comparisons, <=3 swaps, idempotent (optimal number of 
> swaps):
>
> void partition5(ref int[5] a){
>   if(a[0]<a[1]){
>     if(a[2]<a[3]){
>       if(a[0]<a[2]){
>         if(a[1]<a[4]){
>           if(a[1]<a[2]){
>             if(!(a[2]<a[4])){
>               swap(a[4],a[2]);
>             }
>           }else if(a[1]<a[3]){
>             swap(a[1],a[2]);
>           }else{
>             swap(a[3],a[2]);
>             swap(a[1],a[3]);
> <snip>

  That's about what i expected for the actual function (like this) 
to look like. Course the only way to test this is to brute force 
the combinations and confirm it's all in the order they should be.


More information about the Digitalmars-d mailing list