Idempotent partition around median of 5?

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 4 10:48:34 PST 2016


On 02/04/2016 03:46 AM, Xinok wrote:
>
>>  I'm not sure of the max swaps, seems like it could be 4 if done
>> right, but that might be impractical.
>
> I believe that's correct. Each swap can move at least one element into
> it's final position. After three swaps, there may be two elements which
> are still out of place, and swapping them will move them both into their
> final position. Thus, four swaps max. :-)

Three swaps are always enough. Using the first swap, put the median 
where it belongs, and each subsequent swap can be chosen such that it 
moves two elements to their final position.


More information about the Digitalmars-d mailing list