Ternary Search
Given a sorted array, find a target using Ternary search (partition into 3 parts). Discuss which has better worst case performance.
The Idea: Implement binary search, but instead of partitioning in two, we partition into three parts, and recurse onto those depending on where our the value of our current index stands. Even though both have logarithmic time complexity, the most times we split the array, the more if statements that will be needed to iterate through. This is where the constant makes ternary_search perform worse than binary search in the worst case. It will have to iterate down to the finally if statement every time. Consider where n is the size of the array, and k is the number splits. If k == n, then we effectively get k branches of every node. There will only every b a single level in this tree, and checking each branch effectively becomes the same as doing linear search.
Complexity: O(log3(n)) time and O(log3(n)) space
Last updated