Skip to content

Commit f992fc4

Browse files
authored
Add tests, remove main in IterativeTernarySearch (#5668)
1 parent 0a7065d commit f992fc4

File tree

3 files changed

+140
-30
lines changed

3 files changed

+140
-30
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@
10061006
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
10071007
* [InterpolationSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java)
10081008
* [IterativeBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java)
1009+
* [IterativeTernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeTernarySearchTest.java)
10091010
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10101011
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10111012
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4-
import java.util.Arrays;
5-
import java.util.Random;
6-
import java.util.stream.Stream;
74

85
/**
9-
* A iterative version of a ternary search algorithm This is better way to
10-
* implement the ternary search, because a recursive version adds some overhead
11-
* to a stack. But in java the compile can transform the recursive version to
12-
* iterative implicitly, so there are no much differences between these two
13-
* algorithms
6+
* An iterative implementation of the Ternary Search algorithm.
147
*
158
* <p>
16-
* Worst-case performance Θ(log3(N)) Best-case performance O(1) Average
17-
* performance Θ(log3(N)) Worst-case space complexity O(1)
9+
* Ternary search is a divide-and-conquer algorithm that splits the array into three parts
10+
* instead of two, as in binary search. This implementation is iterative, reducing the overhead
11+
* associated with recursive function calls. However, the recursive version can also be optimized
12+
* by the Java compiler to resemble the iterative version, resulting in similar performance.
13+
*
14+
* <p>
15+
* Worst-case performance: Θ(log3(N))<br>
16+
* Best-case performance: O(1)<br>
17+
* Average performance: Θ(log3(N))<br>
18+
* Worst-case space complexity: O(1)
19+
*
20+
* <p>
21+
* This class implements the {@link SearchAlgorithm} interface, providing a generic search method
22+
* for any comparable type.
1823
*
19-
* @author Podshivalov Nikita (https://github.com/nikitap492)
2024
* @see SearchAlgorithm
2125
* @see TernarySearch
2226
* @since 2018-04-13
@@ -25,6 +29,13 @@ public class IterativeTernarySearch implements SearchAlgorithm {
2529

2630
@Override
2731
public <T extends Comparable<T>> int find(T[] array, T key) {
32+
if (array == null || array.length == 0 || key == null) {
33+
return -1;
34+
}
35+
if (array.length == 1) {
36+
return array[0].compareTo(key) == 0 ? 0 : -1;
37+
}
38+
2839
int left = 0;
2940
int right = array.length - 1;
3041

@@ -50,23 +61,4 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
5061

5162
return -1;
5263
}
53-
54-
public static void main(String[] args) {
55-
// just generate data
56-
Random r = new Random();
57-
int size = 100;
58-
int maxElement = 100000;
59-
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
60-
61-
// the element that should be found
62-
Integer shouldBeFound = integers[r.nextInt(size - 1)];
63-
64-
IterativeTernarySearch search = new IterativeTernarySearch();
65-
int atIndex = search.find(integers, shouldBeFound);
66-
67-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
68-
69-
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
70-
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
71-
}
7264
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the IterativeTernarySearch class.
9+
*/
10+
class IterativeTernarySearchTest {
11+
12+
/**
13+
* Test for basic ternary search functionality when the element is found.
14+
*/
15+
@Test
16+
void testTernarySearchFound() {
17+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
18+
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
19+
Integer key = 128;
20+
int expectedIndex = 7; // Index of the key in the array
21+
assertEquals(expectedIndex, ternarySearch.find(array, key), "The index of the found element should be 7.");
22+
}
23+
24+
/**
25+
* Test for ternary search when the element is not present in the array.
26+
*/
27+
@Test
28+
void testTernarySearchNotFound() {
29+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
30+
Integer[] array = {1, 2, 4, 8, 16};
31+
Integer key = 6; // Element not present in the array
32+
assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in the array.");
33+
}
34+
35+
/**
36+
* Test for ternary search with the first element as the key.
37+
*/
38+
@Test
39+
void testTernarySearchFirstElement() {
40+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
41+
Integer[] array = {1, 2, 4, 8, 16};
42+
Integer key = 1; // First element
43+
assertEquals(0, ternarySearch.find(array, key), "The index of the first element should be 0.");
44+
}
45+
46+
/**
47+
* Test for ternary search with the last element as the key.
48+
*/
49+
@Test
50+
void testTernarySearchLastElement() {
51+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
52+
Integer[] array = {1, 2, 4, 8, 16};
53+
Integer key = 16; // Last element
54+
assertEquals(4, ternarySearch.find(array, key), "The index of the last element should be 4.");
55+
}
56+
57+
/**
58+
* Test for ternary search with a single element present.
59+
*/
60+
@Test
61+
void testTernarySearchSingleElementFound() {
62+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
63+
Integer[] array = {1};
64+
Integer key = 1; // Only element present
65+
assertEquals(0, ternarySearch.find(array, key), "The index of the single element should be 0.");
66+
}
67+
68+
/**
69+
* Test for ternary search with a single element not present.
70+
*/
71+
@Test
72+
void testTernarySearchSingleElementNotFound() {
73+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
74+
Integer[] array = {1};
75+
Integer key = 2; // Key not present
76+
assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in the array.");
77+
}
78+
79+
/**
80+
* Test for ternary search with an empty array.
81+
*/
82+
@Test
83+
void testTernarySearchEmptyArray() {
84+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
85+
Integer[] array = {}; // Empty array
86+
Integer key = 1; // Key not present
87+
assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in an empty array.");
88+
}
89+
90+
/**
91+
* Test for ternary search on a large array.
92+
*/
93+
@Test
94+
void testTernarySearchLargeArray() {
95+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
96+
Integer[] array = new Integer[10000];
97+
for (int i = 0; i < array.length; i++) {
98+
array[i] = i * 2;
99+
} // Array from 0 to 19998, step 2
100+
Integer key = 9998; // Present in the array
101+
assertEquals(4999, ternarySearch.find(array, key), "The index of the found element should be 4999.");
102+
}
103+
104+
/**
105+
* Test for ternary search on large array with a non-existent key.
106+
*/
107+
@Test
108+
void testTernarySearchLargeArrayNotFound() {
109+
IterativeTernarySearch ternarySearch = new IterativeTernarySearch();
110+
Integer[] array = new Integer[10000];
111+
for (int i = 0; i < array.length; i++) {
112+
array[i] = i * 2;
113+
} // Array from 0 to 19998, step 2
114+
Integer key = 9999; // Key not present
115+
assertEquals(-1, ternarySearch.find(array, key), "The element should not be found in the array.");
116+
}
117+
}

0 commit comments

Comments
 (0)