Time Complexity of Ternary Search Algorithm

后端 未结 2 1910
离开以前
离开以前 2021-01-07 05:47

I have an assignment that wants me to write an ternary search algorithm and compute its time complexity afterwards. I was able to write an algorithm for it but I couldn\'t c

相关标签:
2条回答
  • 2021-01-07 05:55

    It is Θ(log3(N)). To check how to calculate complexity just check http://en.wikipedia.org/wiki/Big_O_notation

    To read more about ternary search, just check the wikipedia page also: http://en.wikipedia.org/wiki/Ternary_search

    0 讨论(0)
  • 2021-01-07 06:10

    At each step, you are reducing the size of the searchable range by a constant factor (in this case 3). If you find your element after n steps, then the searchable range has size N = 3n. Inversely, the number of steps that you need until you find the element is the logarithm of the size of the collection. That is, the runtime is O(log N). A little further thought shows that you can also always construct situations where you need all those steps, so the worst-case runtime is actually Θ(log N).

    0 讨论(0)
提交回复
热议问题