Why use binary search if there's ternary search?

前端 未结 15 1686
别跟我提以往
别跟我提以往 2020-12-02 12:43

I recently heard about ternary search in which we divide an array into 3 parts and compare. Here there will be two comparisons but it reduces the array to n/3. Why don\'t p

15条回答
  •  情歌与酒
    2020-12-02 12:48

    What makes you think Ternary search should be faster?

    Average number of comparisons:

    in ternary search = ((1/3)*1 + (2/3)*2) * ln(n)/ln(3) ~ 1.517*ln(n)
    in binary search  =                   1 * ln(n)/ln(2) ~ 1.443*ln(n).
    

    Worst number of comparisons:

    in ternary search = 2 * ln(n)/ln(3) ~ 1.820*ln(n)
    in binary search  = 1 * ln(n)/ln(2) ~ 1.443*ln(n).
    

    So it looks like ternary search is worse.

提交回复
热议问题