Which the most effecient way to find a median of three integer number without using an array like the example below:
int[] median = {int a, int b,int c};
Array.Sort(median);
int medianValue = median[1];
The fastest way I know of is to use
max(min(a, b), min(max(a, b), c))
I'm trusting that C# has optimisations for min
and max
taking two arguments. This will be quicker than taking if
statements due to branching.
There are other tricks: you can implement min and max using XOR and < but I doubt that has any benefits on modern architectures.