Ternary Search Algorithm

Distribute Education like Computer Geek

Definition

  1. Ternary search is a searching algorithm used to find a specific element within a sorted list or array.
  2. It works by repeatedly dividing the search range into three parts and then narrowing down the search based on comparisons with the elements at the two dividing points.
Ternary Search Algorithm
Binary Search Vs Ternary Search
  1. This process continues until the desired element is found or the search range is reduced to a single element.

Working of Ternary Search

Imagine you have a sorted list of numbers and you want to find a specific number, let’s say 36. Instead of looking at each number one by one, ternary search helps you narrow down your search range quickly.

1. Dividing into Three Parts

You start by dividing the list into three parts. You pick two points, let’s call them left and right, which divide the list into three segments.

2. Comparing with Midpoints

You compare the number you’re looking for (25) with the elements at the left and right points. This gives you some information about where your target might be located.

3. Adjusting the Search Range

Based on the comparisons, you have three possibilities

  1. If your target is smaller than the element at left, you know it must be in the left segment.
  2. If it’s larger than the element at right, you know it’s in the right segment.
  3. If it’s neither, it’s likely in the middle segment.
4. Reducing the Search Range

You narrow down your search range based on the comparisons. If your target is in the left or right segment, you ignore the other segments. If it’s in the middle segment, you update the left and right points to focus on a smaller range.

5. Repeat the Process

You repeat the above steps with the new, smaller range. You pick new left and right points, compare with the middle elements, and adjust the range again.

6. Continuing until Found

You keep repeating the process until you either find the number 25 or narrow down the search range to just one number. Once you’ve done that, you’ve found your target!

.

Algorithm

  1. Imagine you have a sorted list, like numbers from small to big.
  2. First, you find two points that divide the list into three parts: left, middle, and right.
  3. You compare the item you’re looking for with the items at the left and right points.
  4. If your item is smaller than the left one, you know it must be in the left part.
  5. If it’s bigger than the right one, it must be in the right part.
  6. If it’s neither, it’s likely in the middle part.

.

Source Code for Ternary Search

Time Complexity

Ternary search can be really fast, but it’s not always better than binary search.

  1. In the best case, the time complexity is O(1) for both ternary search and binary search.
  2. Some of you are thinking that in the average-case, ternary search has less time complexity than binary search because binary search searches the target element in only 1 place (The middle) and ternary search searches the target element in two places (1/3 and 2/3 places). But this is wrong. In an iteration, ternary search has two comparison and binary search has only 1 comparison.
  3. In the worst case, it takes about log3(n) steps, which is slower than log2(n) steps in binary search.
Space Complexity

The space needed for ternary search is pretty low O(1) because it only requires a few variables to keep track of the positions and comparisons.

 

Advantages of Ternary search
  1. Can be faster than binary search when the list has very few possibilities.
  2. Useful when the list isn’t evenly divided, or you want to find the local maximum or minimum.
Disadvantages of Ternary Search
  1. Slower than binary search in many cases.
  2. Works well only on sorted lists.
  3. More complicated than binary search.

Test Yourself

Q1- Explain the concept of the ternary search algorithm.

The ternary search algorithm is a searching technique used to find a specific element within a sorted list or array. It divides the search range into three parts and compares the target element with elements at two dividing points. Based on these comparisons, it narrows down the search range until the desired element is found or the range becomes a single element.

Q2- Describe a scenario where the ternary search algorithm can be applied effectively.

Ternary search is effective when searching for the maximum or minimum value in a unimodal function. For instance, in optimizing mathematical functions, the algorithm can be used to quickly find the optimal point where a certain value is maximized or minimized.

Q3- Explain the time complexity of the ternary search algorithm.

In the best case, where the target is found immediately, the time complexity is O(1). In the worst case, the algorithm requires approximately log3(n) steps to narrow down the search range to one element.

Q4- Describe a real-life situation where ternary search can be applied creatively.

Ternary search can be applied in identifying optimal tuning parameters in machine learning algorithms. It helps find the parameters that yield the best performance by narrowing down the search space of potential values.

Q5- In which scenario is ternary search less efficient?
  1. When the list is sorted
  2. When the list is unimodal
  3. When the search space is evenly divided
  4. When the search space is not evenly divided

Ans – (4)

Explanation – Ternary search’s efficiency decreases when the search space is not evenly divided into three parts.

Q6- Ternary search is particularly useful for finding
  1. Elements in an unsorted list
  2. Local extremes in unimodal functions
  3. Elements in a sorted list
  4. Global extremes in non-unimodal functions

Ans – (2)

Explanation – Ternary search excels in finding local maximum or minimum points in unimodal functions.

Q7- Ternary search is more efficient than binary search when:
  1. The list is unsorted
  2. The list has a small number of elements
  3. The search space is evenly divided
  4. The list is sorted

Ans – (2)

Explanation – Ternary search can be faster than binary search when the search space is small.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha