Skip to content

Implement iterative ternary search #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/com/search/IterativeTernarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

}
26 changes: 26 additions & 0 deletions src/test/java/com/search/IterativeTernarySearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.search;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class IterativeTernarySearchTest {

@Test
void testIterativeTernarySearch() {
Integer[] arr1 = {1, 2, 3, 5, 8, 13, 21, 34, 55};
Assertions.assertEquals(2, IterativeTernarySearch.find(arr1, 3), "Incorrect index");
Assertions.assertEquals(0, IterativeTernarySearch.find(arr1, 1), "Incorrect index");
Assertions.assertEquals(8, IterativeTernarySearch.find(arr1, 55), "Incorrect index");
Assertions.assertEquals(-1, IterativeTernarySearch.find(arr1, -2), "Incorrect index");
Assertions.assertEquals(-1, IterativeTernarySearch.find(arr1, 4), "Incorrect index");

String[] arr2 = {"A", "B", "C", "D"};
Assertions.assertEquals(2, IterativeTernarySearch.find(arr2, "C"), "Incorrect index");
Assertions.assertEquals(1, IterativeTernarySearch.find(arr2, "B"), "Incorrect index");
Assertions.assertEquals(-1, IterativeTernarySearch.find(arr2, "F"), "Incorrect index");

String[] arr3 = {};
Assertions.assertEquals(-1, IterativeTernarySearch.find(arr3, ""), "Incorrect index");
}

}