median of three values strategy

前端 未结 8 1626
粉色の甜心
粉色の甜心 2020-11-29 23:08

What is the median of three strategy to select the pivot value in quick sort?

I am reading it on the web, but I couldn\'t figure it out what exactly it is? And also

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 23:53

    I think rearranging the values in the array is not necessary for just three values. Just compare all of them by subtracting; then you can decide which one is the median value:

    // javascript:
    var median_of_3 = function(a, b, c) {
        return ((a-b)*(b-c) > -1 ? b : ((a-b)*(a-c) < 1 ? a : c));
    }
    

提交回复
热议问题