二元搜索与三元搜索 [英] Binary Search vs Ternary Search

查看:185
本文介绍了二元搜索与三元搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就时间时间空间而言,二元搜索优于三元搜索吗?

In terms of time and space complexity, is binary search better than ternary search?

推荐答案

两者都具有恒定的空间,但是三元搜索的O时间大是Log_3 N,而不是二元搜索的Log_2 N 因为log_b(N)= log_x(N)/ log_x(b)来登录(N)。

Both have constant space, but big O time for Ternary Search is Log_3 N instead of Binary Search's Log_2 N which both come out to log(N) since log_b(N) = log_x(N)/log_x(b).

实际上,由于您使用了三元搜索必须在每个步骤进行额外的比较,通常情况下,这会导致整体上进行更多的比较。 2 * Log_3(N)比较vs Log_2(N)比较。

In practice Ternary Search isn't used because you have to do an extra comparison at each step, which in the general case leads to more comparisons overall. 2 * Log_3(N) comparisons vs Log_2(N) comparisons.

这篇关于二元搜索与三元搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆