Created
August 25, 2017 23:28
-
-
Save thejoshwolfe/6df79a3c5b3e7b30ae184ced0f7eb9c0 to your computer and use it in GitHub Desktop.
given 3 ints, return the median
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int median3(int a, int b, int c) { | |
int index = | |
((a < b) << 0) | | |
((b < c) << 1) | | |
((c < a) << 2); | |
switch (index) { | |
case 0: unreachable(); | |
case 1: return c; | |
case 2: return a; | |
case 3: return b; | |
case 4: return b; | |
case 5: return a; | |
case 6: return c; | |
case 7: unreachable(); | |
} | |
unreachable(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment