Ternary Search in C
Last Updated :
05 Oct, 2023
When searching for a specific element in a sorted dataset, many programmers are familiar with binary search. Binary search efficiently narrows down the search space by dividing it into two halves repeatedly. But there’s another search algorithm that can be even more efficient in certain scenarios.
Ternary search is a searching algorithm that efficiently locates an element in a sorted dataset by dividing it into three parts, rather than two, as in binary search. This additional partitioning can lead to a faster search in some cases. While binary search reduces the search space by half in each iteration, ternary search narrows it down to two-thirds.
Prerequisites: C Loops, Arrays, Binary Search and Recursion
Working of Ternary Search
Ternary search is based on the divide-and-conquer strategy. It works as follows:
- Divide the dataset into three parts.
- Compare the target element with the elements at the two division points.
- Depending on the comparisons, you can eliminate one-third of the dataset.
- Repeat the process until you find the target element or exhaust the search space.
Let’s illustrate this with a simple example.
Consider a sorted array ‘arr’:
arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
Suppose we want to find the index of the element 13 using ternary search. The algorithm proceeds as follows:
- In each step, we divide the array into three parts and compare the middle elements.
- Based on the comparison, we eliminated one-third of the dataset.
- This process continues until we find the target element or determine it doesn’t exist in the dataset.
C Program for Ternary Search
Here’s a simple C code example to perform ternary search:
C
#include <stdio.h>
int ternarySearch( int arr[], int left, int right,
int target)
{
if (left <= right) {
int mid1 = left + (right - left) / 3;
int mid2 = right - (right - left) / 3;
if (arr[mid1] == target)
return mid1;
if (arr[mid2] == target)
return mid2;
if (target < arr[mid1])
return ternarySearch(arr, left, mid1 - 1,
target);
else if (target > arr[mid2])
return ternarySearch(arr, mid2 + 1, right,
target);
else
return ternarySearch(arr, mid1 + 1, mid2 - 1,
target);
}
return -1;
}
int main()
{
int arr[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
int target = 13;
int n = sizeof (arr) / sizeof (arr[0]);
int result = ternarySearch(arr, 0, n - 1, target);
if (result != -1)
printf ( "Element %d found at index %d\n" , target,
result);
else
printf ( "Element %d not found in the array\n" ,
target);
return 0;
}
|
Output
Element 13 found at index 6
Application and Benefits
Ternary search is particularly useful in scenarios where you have a large, sorted dataset, and you want to find an element efficiently. Here are some key benefits:
- Efficiency: Ternary search reduces the search space by one-third in each iteration, making it faster than Linear search. In some cases, It is faster than binary search, especially when the target element is closer to the beginning of the dataset.
- Simplicity: The algorithm is easy to understand and implement, making it accessible to programmers of all levels.
- Guaranteed Success: Ternary search guarantees success if the target element is present in the dataset. It continues until the search space is empty.
Similar Reads:
Share your thoughts in the comments
Please Login to comment...