# Algorithm for min/max when order in row

I have 5 numbers which belongs to Z. I am trying to create an algorithm which gives the min and max from the set in only 6 iterations (the least we need to solve the problem).

The purpose in my method is set all numbers in a row from min to max, then the first is min and last is max.

for three numbers is like if a>b then we use a temporary variable d=b b=a a=d now i made a < b for sure. the next iteration is if b>c we do like above, then again a>c and last iteration is a>b again. in this method we get an order for any three numbers when 'a' is min and 'c' is max.

However, for 5 numbers I have an problem when I`m trying to make the numbers in row with only 6 iterations.

Any ideas?

• I think you need at least $7$ comparisons to sort $5$ numbers, because with two possible results from each comparison can distinguish $2^6=64$ different orderings with $6$ comparisions but there are $5!=120$ possible orderings. – gandalf61 Oct 30 '18 at 15:35