forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TernarySearch.java
42 lines (32 loc) · 1.59 KB
/
TernarySearch.java
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
package search;
public class TernarySearch {
/*
* Ternary Search using recursion
* Time complexity - Olog(n)
* Memory Complexity - O(logn) - as it will consume memory on the stack
*
* Ternary search is a fast search algorithm which works on the principle of divide & conquer.
* It splits an array into three smaller arrays by finding the median value in the array.
* The two different medians are found by adding the median value to the lowerIndex and by subtracting the upperIndex
* by the median. If the value is greater than the median then the lower bound of the array is increased and if the value is less than the median then the upper bound of the array is decreased.
* It can be used only for sorted arrays.
* Ternary search reduces the array into half thus reducing the count of comparisons to be made
*
* */
public static int ternarySearch(int n, int[] arr, int lowerIndex, int upperIndex) {
if(n < arr[lowerIndex] || n > arr[upperIndex]) return -1;
int median = (upperIndex - lowerIndex) / 3;
int med1 = lowerIndex + median;
int med2 = upperIndex - median;
if(arr[med1] == n) return med1;
if(arr[med2] == n) return med2;
if(arr[med1] < n) return ternarySearch(n, arr, med1+1, upperIndex);
if(arr[med2] > n) return ternarySearch(n, arr, lowerIndex, med2-1);
return -1;
}
public static void main(String[] args) {
int[] arr = {1,11,23,34,45,56,67,78,89,90};
int n = 90;
System.out.println(ternarySearch(n, arr, 0, arr.length-1));
}
}