user1769735 - 6 months ago 29

Java Question

I have to put 3 values in correct order and print them out to the console.

A solution is to put them into an array and then sort them, but I remember (from school times) that there was faster to compare and order them, however I can't find the correct comparison order.

Could you please show me how to compare 3 values with the minimum number of

`if`

Answer

There is no point in optimizing this. It will not gain any speed. O(n!) for 3 is still only 3*2 = 6 operations. Even O(2^n) is going to be 8. You could really do whatever it takes to sort these 3 values and not see a difference in performance.

*edit*

```
int a, b, c, min, max, med;//assume values are there for a b c
if( a > b ){
if( a > c ){
max = a;
if( b > c ){
med = b;
min = c;
}else{
med = c;
min = b;
}
}else{
med = a;
max = c;
min = b;
}
}else{
if( b > c ){
max = b;
if( a > c ){
med = a;
min = c;
}else{
med = c;
min = a;
}
}else{
med = b;
max = c;
min = a;
}
}
```