From c0de49fb5a27ae252a3f67d9949c74003609f35c Mon Sep 17 00:00:00 2001 From: Subham Singh Date: Sun, 6 Oct 2024 19:07:07 +0530 Subject: [PATCH] Added Ternary Search Program --- TernarySearch.java | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 TernarySearch.java diff --git a/TernarySearch.java b/TernarySearch.java new file mode 100644 index 00000000..329b7b5f --- /dev/null +++ b/TernarySearch.java @@ -0,0 +1,69 @@ +import java.util.Scanner; + +public class TernarySearch { + + // Function to perform ternary search on a sorted array + public static int ternarySearch(int[] arr, int left, int right, int key) { + if (right >= left) { + // Find the two mid points + int mid1 = left + (right - left) / 3; + int mid2 = right - (right - left) / 3; + + // Check if key is at any mid + if (arr[mid1] == key) { + return mid1; + } + if (arr[mid2] == key) { + return mid2; + } + + // Key lies in left one-third + if (key < arr[mid1]) { + return ternarySearch(arr, left, mid1 - 1, key); + } + // Key lies in right one-third + else if (key > arr[mid2]) { + return ternarySearch(arr, mid2 + 1, right, key); + } + // Key lies in middle one-third + else { + return ternarySearch(arr, mid1 + 1, mid2 - 1, key); + } + } + + // Key not found + return -1; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Taking array input + System.out.print("Enter the number of elements in the array: "); + int n = scanner.nextInt(); + + int[] arr = new int[n]; + System.out.println("Enter the elements of the array in sorted order:"); + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + // Taking the key input + System.out.print("Enter the element to search for: "); + int key = scanner.nextInt(); + + // Performing ternary search + int result = ternarySearch(arr, 0, arr.length - 1, key); + + if (result != -1) { + System.out.println("Element found at index: " + result); + } else { + System.out.println("Element not found in the array."); + } + + scanner.close(); + } +} + + +