Skip to content

Commit c002090

Browse files
committed
Added a iterative version of ternary search algorithm and some minor changes in documentation
1 parent 066adc7 commit c002090

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package search;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
import java.util.stream.Stream;
6+
7+
import static java.lang.String.format;
8+
9+
/**
10+
*
11+
* A iterative version of a ternary search algorithm
12+
* This is better way to implement the ternary search, because a recursive version adds some overhead to a stack.
13+
* But in java the compile can transform the recursive version to iterative implicitly,
14+
* so there are no much differences between these two algorithms
15+
*
16+
* Worst-case performance Θ(log3(N))
17+
* Best-case performance O(1)
18+
* Average performance Θ(log3(N))
19+
* Worst-case space complexity O(1)
20+
*
21+
*
22+
* @author Podshivalov Nikita (https://github.com/nikitap492)
23+
* @since 2018-04-13
24+
*
25+
* @see SearchAlgorithm
26+
* @see TernarySearch
27+
*
28+
*/
29+
30+
public class IterativeTernarySearch implements SearchAlgorithm {
31+
32+
33+
@Override
34+
public <T extends Comparable<T>> int find(T[] array, T key) {
35+
int left = 0;
36+
int right = array.length - 1;
37+
while (true) {
38+
int leftCmp = array[left].compareTo(key);
39+
int rightCmp = array[right].compareTo(key);
40+
if (leftCmp == 0) return left;
41+
if (rightCmp == 0) return right;
42+
43+
int leftThird = left + (right - left) / 3;
44+
int rightThird = right - (right - left) /3;
45+
46+
if (array[leftThird].compareTo(key) <= 0) left = leftThird;
47+
else right = rightThird;
48+
}
49+
50+
}
51+
52+
53+
public static void main(String[] args) {
54+
//just generate data
55+
Random r = new Random();
56+
int size = 100;
57+
int maxElement = 100000;
58+
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[]::new);
59+
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.println(format("Should be found: %d. Found %d at index %d. An array length %d"
68+
, shouldBeFound, integers[atIndex], atIndex, size));
69+
70+
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
71+
System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));
72+
73+
}
74+
75+
76+
}

Sorts/src/sort/SortAlgorithm.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55

66
/**
7-
* The common interface of most algorithms
7+
* The common interface of most sorting algorithms
88
*
99
* @author Podshivalov Nikita (https://github.com/nikitap492)
1010
*

0 commit comments

Comments
 (0)