Skip to main content
added 61 characters in body
Source Link
par
  • 121
  • 4

I came up with this answer independently but it is effectively the same as JS1's (so upvote that one not mine), but written slightly differently. Generally I prefer not to use else when an if statement is guaranteed to return and I prefer ternary ?: syntax when it's simple to understand over if/else.

int find_median(int a, int b, int c) {
    if (a < b) {
        if (b < c) return b;                // a b c
        if (c < a) return a;                // c a b
        return c;                           // a c b
    }
    if (b < c) { return a < c ? a : c; }       // b a c : b c a

    return b;                               // c b a
}

This method results in only three comparisons at most (obviously some assumptions are made about inputs but that was stated in the question), no function calls, and no wasted instructions dereferencing pointers or storing variables. It is vastly superior to any of the methods that require sorting. Again, this was put forth before me by JS1 so I feel his/her answer should be the accepted one.

I came up with this answer independently but it is effectively the same as JS1's (so upvote that one not mine), but written slightly differently. Generally I prefer not to use else when an if statement is guaranteed to return and I prefer ternary ?: syntax when it's simple to understand over if/else.

int find_median(int a, int b, int c) {
    if (a < b) {
        if (b < c) return b;                // a b c
        if (c < a) return a;                // c a b
        return c;                           // a c b
    }
    if (b < c) { return a < c ? a : c; }    // b a c : b c a

    return b;                               // c b a
}

This method results in only three comparisons at most (obviously some assumptions are made about inputs but that was stated in the question) and is vastly superior to any of the methods that require sorting. Again, this was put forth before me by JS1 so I feel his/her answer should be the accepted one.

I came up with this answer independently but it is effectively the same as JS1's (so upvote that one not mine), but written slightly differently. Generally I prefer not to use else when an if statement is guaranteed to return and I prefer ternary ?: syntax when it's simple to understand over if/else.

int find_median(int a, int b, int c) {
    if (a < b) {
        if (b < c) return b;                // a b c
        if (c < a) return a;                // c a b
        return c;                           // a c b
    }
    if (b < c) return a < c ? a : c;        // b a c : b c a

    return b;                               // c b a
}

This method results in three comparisons at most (obviously some assumptions are made about inputs but that was stated in the question), no function calls, and no wasted instructions dereferencing pointers or storing variables. It is vastly superior to any of the methods that require sorting. Again, this was put forth before me by JS1 so I feel his/her answer should be the accepted one.

Source Link
par
  • 121
  • 4

I came up with this answer independently but it is effectively the same as JS1's (so upvote that one not mine), but written slightly differently. Generally I prefer not to use else when an if statement is guaranteed to return and I prefer ternary ?: syntax when it's simple to understand over if/else.

int find_median(int a, int b, int c) {
    if (a < b) {
        if (b < c) return b;                // a b c
        if (c < a) return a;                // c a b
        return c;                           // a c b
    }
    if (b < c) { return a < c ? a : c; }    // b a c : b c a

    return b;                               // c b a
}

This method results in only three comparisons at most (obviously some assumptions are made about inputs but that was stated in the question) and is vastly superior to any of the methods that require sorting. Again, this was put forth before me by JS1 so I feel his/her answer should be the accepted one.