Open In App

Time and Space Complexity of Ternary Search

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The time complexity of Ternary Search is O(log3 N), where N is the size of the array. In terms of space complexity, ternary search requires only O(1) auxiliary space, as it operates directly on the given array without creating any additional data structures.

Feature

Ternary Search

Time Complexity

O(log3N)

Auxiliary Space

O(log3 N)

Let’s explore the detailed time and space complexity of the Ternary Search:

Time Complexity of Ternary Search:

Best Case Time Complexity: O(1)

  • When the target element is found at the midpoint of the search interval.

Average Case Time Complexity: O(log3 n)

  • When the target element is not found at the midpoint, but is found within the search interval. This is the average case because, on average, the search interval will be divided into three equal parts, and the target element will be found within one of these parts.

Worst Case Time Complexity: O(log3 n)

  • When the target element is not found within the search interval. In this case, the search interval will be repeatedly divided into three equal parts until it becomes empty. The worst case occurs when the target element is not in the array, and the search interval is divided into three equal parts at each step.

Auxiliary Space of Ternary Search:

The auxiliary space of ternary search is O(log3 N), where N is the number of elements in the ternary search tree. This complexity is primarily due to the recursive call stack.

Recursive Calls Stack: O(log3 N)

Ternary search uses recursion to traverse the ternary search tree. Each recursive call creates a new stack frame, which requires additional memory space. The maximum depth of the recursion is equal to the height of the ternary search tree, which can be as large as O(log3 N).


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads