Skip to content

Commit 3e64771

Browse files
author
Christian Bender
authored
Merge pull request #417 from nikitap492/search
Thanks! Keep it up!
2 parents b082ecc + ecfd0f0 commit 3e64771

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
38+
while (right > left) {
39+
40+
int leftCmp = array[left].compareTo(key);
41+
int rightCmp = array[right].compareTo(key);
42+
if (leftCmp == 0) return left;
43+
if (rightCmp == 0) return right;
44+
45+
int leftThird = left + (right - left) / 3 + 1;
46+
int rightThird = right - (right - left) / 3 - 1;
47+
48+
49+
if (array[leftThird].compareTo(key) <= 0) {
50+
left = leftThird;
51+
} else {
52+
right = rightThird;
53+
}
54+
}
55+
56+
return -1;
57+
}
58+
59+
60+
public static void main(String[] args) {
61+
//just generate data
62+
Random r = new Random();
63+
int size = 100;
64+
int maxElement = 100000;
65+
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement))
66+
.limit(size)
67+
.sorted()
68+
.toArray(Integer[]::new);
69+
70+
71+
//the element that should be found
72+
Integer shouldBeFound = integers[r.nextInt(size - 1)];
73+
74+
IterativeTernarySearch search = new IterativeTernarySearch();
75+
int atIndex = search.find(integers, shouldBeFound);
76+
77+
System.out.println(format("Should be found: %d. Found %d at index %d. An array length %d",
78+
shouldBeFound, integers[atIndex], atIndex, size));
79+
80+
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
81+
System.out.println(format("Found by system method at an index: %d. Is equal: %b",
82+
toCheck, toCheck == atIndex));
83+
84+
}
85+
86+
87+
}

Sorts/src/sort/SortAlgorithm.java

Lines changed: 1 addition & 1 deletion
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)