This is a Java Program to Implement Ternary Search Algorithm. A ternary search algorithm is a technique in computer science for finding the minimum or maximum of an increasing or decreasing 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.
Here is the source code of the Java Program to Implement Ternary Search Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | /** ** Java Program to Implement Ternary Search Algorithm **/ import java.util.Scanner; /** Class TernarySearch **/ public class TernarySearch { /** call function **/ public static int ternarySearch ( int [] A, int value) { return ternarySearch(A, value, 0 , A.length - 1 ); } /** TernarySearch function **/ public static int ternarySearch ( int [] A, int value, int start, int end) { if (start > end) return - 1 ; /** First boundary: add 1/3 of length to start **/ int mid1 = start + (end-start) / 3 ; /** Second boundary: add 2/3 of length to start **/ int mid2 = start + 2 *(end-start) / 3 ; if (A[mid1] == value) return mid1; else if (A[mid2] == value) return mid2; /** Search 1st third **/ else if (value < A[mid1]) return ternarySearch (A, value, start, mid1- 1 ); /** Search 3rd third **/ else if (value > A[mid2]) return ternarySearch (A, value, mid2+ 1 , end); /** Search middle third **/ else return ternarySearch (A, value, mid1,mid2); } /** Main method **/ public static void main(String[] args) { Scanner scan = new Scanner( System.in ); System.out.println( "Ternary Search Test\n" ); int n, i; /** Accept number of elements **/ System.out.println( "Enter number of integer elements" ); n = scan.nextInt(); /** Create integer array on n elements **/ int arr[] = new int [ n ]; /** Accept elements **/ System.out.println( "\nEnter " + n + " sorted integer elements" ); for (i = 0 ; i < n; i++) arr[i] = scan.nextInt(); System.out.println( "\nEnter element to search for : " ); int key = scan.nextInt(); int result = ternarySearch(arr, key); if (result == - 1 ) System.out.println( "\n" + key + " element not found" ); else System.out.println( "\n" + key + " element found at position " + result); } } |
1 2 3 4 5 6 7 8 9 10 11 12 | Ternary Search Test Enter number of integer elements 10 Enter 10 sorted integer elements 2 5 15 24 31 47 59 61 79 97 Enter element to search for : 24 24 element found at position 3 |
Related posts:
Java Program to Solve the Fractional Knapsack Problem
Converting String to Stream of chars
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Handling URL Encoded Form Data in Spring REST
Instance Profile Credentials using Spring Cloud
Introduction to the Functional Web Framework in Spring 5
Template Engines for Spring
Java Program to Implement Find all Cross Edges in a Graph
Removing all Nulls from a List in Java
Map to String Conversion in Java
Marker Interface trong Java
Java Program to Create a Balanced Binary Tree of the Incoming Data
Finding Max/Min of a List or Collection
Java 8 Predicate Chain
Java Program to Implement Rope
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
A Guide to Java HashMap
Java Program to Perform Naive String Matching
The Java 8 Stream API Tutorial
HttpClient Connection Management
Java Program to Check whether Directed Graph is Connected using DFS
Check If a File or Directory Exists in Java
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Using JWT with Spring Security OAuth (legacy stack)
Java TreeMap vs HashMap
Collect a Java Stream to an Immutable Collection
Object cloning trong java
Iterable to Stream in Java
A Guide to Java SynchronousQueue
Spring WebClient and OAuth2 Support