Java: Find a specified element in a given array of elements using Ternary search
Write a Java program to find a specified element in a given array of elements using Ternary search.
From Wikipedia, a ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two thirds. A ternary search is an example of a divide and conquer algorithm.
Sample Solution:
Java Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] nums = new int[]{0,1,2,3,5,7,9,12,15,17,18,21,25,32,52,54,75,89,90,93,97,104,120};
System.out.println("Original array:");
System.out.println(Arrays.toString(nums));
System.out.println("Input an element to search:");
int val = scan.nextInt();
int position = ternary_search(nums, val, 0, nums.length-1);
if(position == -1)
System.out.println("\n" +val+ " Element not found");
else
System.out.println("\n"+ val +" element found at position "+ position);
}
static int ternary_search(int[] nums, int val, int first_element, int last_element)
{
if(first_element > last_element)
{
return -1;
}
int mid1_element = first_element + (last_element - first_element) / 3;
int mid2_element = first_element + 2*(last_element - first_element) / 3;
if(val == nums[mid1_element])
{
return mid1_element;
}
else if(val == nums[mid2_element])
{
return mid2_element;
}
else if(val > nums[mid1_element])
{
first_element = mid1_element+1;
}
else if(val < nums[mid2_element])
{
last_element = mid2_element-1;
}
return ternary_search(nums, val, first_element, last_element);
}
}
Sample Output:
Original array: [0, 1, 2, 3, 5, 7, 9, 12, 15, 17, 18, 21, 25, 32, 52, 54, 75, 89, 90, 93, 97, 104, 120] Input an element to search: 25 25 element found at position 12
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Java program to implement ternary search on a unimodal function to find its maximum value.
- Write a Java program to perform ternary search on a sorted array by dividing it into three segments to locate a target element.
- Write a Java program to modify ternary search for arrays with duplicate elements to return the first occurrence index.
- Write a Java program to extend ternary search to work on a sorted list of custom objects using a provided comparator.
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to find a specified element in a given sorted array of elements using Exponential search.
Next: Write a Java program to find the row, column position of a specified number (row, column position) in a given 2-dimensional array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics