Fastest way to sort 3 values in Java [closed]

bubble sort would have only 3 compare ops, and 6 assignments at worst case (it will be very similar if not identical to the behavior of insertion sort in this case):

if (a > b)
   swap(a,b)
if (b > c)
   swap(b,c)
if (a > b)
   swap(a,b)
print a,b,c

It cannot be done in less then 3 compares because there are n!=6 possible permutations for the array, and ceil(log_2(n!)) = 3

Leave a Comment