sorting int array with only 3 elements

clamp picture clamp · Jan 25, 2011 · Viewed 11.1k times · Source

I have this array:

int [] myarray =  {17, 6, 8};

What is the optimal way to sort this array, in pseudocode?

Thanks!

Answer

nan picture nan · Jan 25, 2011

I think this should be quite fast (ascending order):

if (el1 > el2) Swap(el1,el2)
if (el2 > el3) Swap(el2,el3)
if (el1 > el2) Swap(el1,el2)