median of three values strategy

前端 未结 8 1607
粉色の甜心
粉色の甜心 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:55

    This strategy consists of choosing three numbers deterministically or randomly and then use their median as pivot.

    This would be better because it reduces the probability of finding "bad" pivots.

    0 讨论(0)
  • 2020-11-29 23:55

    Think simple... Python example....

    def bigger(a,b): #Find the bigger of two numbers ...
        if a > b:
            return a
        else:
            return b
    
    def biggest(a,b,c): #Find the biggest of three numbers ...
        return bigger(a,bigger(b,c))
    
    def median(a,b,c): #Just dance!
        x = biggest(a,b,c)
        if x == a:
            return bigger(b,c)
        if x == b:
            return bigger(a,c)
        else:
            return bigger(a,b)
    
    0 讨论(0)
提交回复
热议问题