-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathIterativeTernarySearch.java
51 lines (42 loc) · 1.51 KB
/
IterativeTernarySearch.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
43
44
45
46
47
48
49
50
51
package com.search;
/**
* A iterative version of a ternary search algorithm
* This is better way to implement the ternary search, because a recursive version adds some overhead to a stack.
* But in java the compile can transform the recursive version to iterative implicitly,
* so there are no much differences between these two algorithms
* <p>
* Worst-case performance Θ(log3(N))
* Best-case performance O(1)
* Average performance Θ(log3(N))
* Worst-case space complexity O(1)
*/
public final class IterativeTernarySearch {
/**
* @param array The **Sorted** array in which we will search the element.
* @param value The value that we want to search for.
* @return The index of the element if found.
* Else returns -1.
*/
public static <T extends Comparable<T>> int find(T[] array, T value) {
int left = 0;
int right = array.length - 1;
while (right > left) {
int leftCompareTo = array[left].compareTo(value);
int rightCompareTo = array[right].compareTo(value);
if (leftCompareTo == 0) {
return left;
}
if (rightCompareTo == 0) {
return right;
}
int leftThird = left + (right - left) / 3 + 1;
int rightThird = right - (right - left) / 3 - 1;
if (array[leftThird].compareTo(value) <= 0) {
left = leftThird;
} else {
right = rightThird;
}
}
return -1;
}
}