Ternary
Ternary search is a divide-and-conquer algorithm used for finding the position of a specific value within an array, especially when the array is sorted.
pseudocode:
It's similar to binary search, but instead of dividing the array into halves, it divides it into thirds.
In this example, the ternarySearch
function takes an array arr
and a target value target
as input and returns the index of the target value if found, or -1 if not found.
The algorithm divides the array into three parts (mid1
, mid2
) and compares the target value with elements at these two midpoints.
Depending on the comparison, it narrows down the search space by updating the left
and right
pointers.
This process continues until the target value is found or the search space is exhausted.
Last updated