Skip to content

Commit c29672d

Browse files
committed
Ternary search README and code updated
1 parent 427a9d4 commit c29672d

File tree

9 files changed

+47
-3
lines changed

9 files changed

+47
-3
lines changed

Searching/Ternary Search/README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
11
# Ternary Search
22

3-
**Ternary search** algorithm is a technique in computer science for finding the minimum or maximum of a [unimodal](https://en.wikipedia.org/wiki/Unimodality) function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two-thirds.
4-
Unimodal functions are functions that, have a single highest value.
3+
**Ternary search** algorithm is a technique in computer science for finding the minimum or maximum of a [unimodal](https://en.wikipedia.org/wiki/Unimodality) function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two-thirds.
4+
Unimodal functions are functions that, have a single highest/lowest value.
55

6+
Suppose we have a function f(x) with only one max point between A and B. We want to find the point (M, f(M)) where f(M) is the maximum between A and B.
7+
8+
We split the range from A to B into three intervals. At every iteration of our algorithm, we can narrow down the range by 1/3 and we have a new interval. At every step, we can remove one of the intervals based on the following:
9+
10+
Let m<sub>1</sub> by 1/3 of the way from A and B and let m<sub>2</sub> be 2/3 of the way from B.
11+
12+
![Ternary Search](./images/ternarysearch.png)
13+
14+
**Case 1 :** f(m1) < f(m2)
15+
16+
* **Case 1.1:** m1 < m2 < M, so m1 < M
17+
18+
![Ternary Search 1.1](./images/ternarycase11.png)
19+
20+
* **Case 1.2:** m1 < M < m2, so m1 < M
21+
22+
![Ternary Search 1.2](./images/ternarycase12.png)
23+
24+
* **Case 1.3:** M < m1 < m2 is not possible.
25+
26+
Thus if f(m1) < f(m2), then m1 < M, so we only need to search from m1 to B.
27+
28+
![Ternary Search 1](./images/ternarycase1.png)
29+
30+
**Case 2:** f(m1) >= f(m2)
31+
32+
* **Case 2.1:** m1 < M < m2, so M < m2
33+
34+
![Ternary Search 2.1](./images/ternarycase21.png)
35+
36+
* **Case 2.2:** M < m1 < m2, so M < m2
37+
38+
![Ternary Search 2.2](./images/ternarycase22.png)
39+
40+
* **Case 2.3:** m1 < m2 < M is not possible
41+
42+
Thus, if f(m1) >= f(m2), then M < m2, so we only need to search from A to m2.
43+
44+
![Ternary Search 2](./images/ternarycase2.png)
45+
46+
Therefore, based on the values of f(m1) and f(m2), we can always remove a third of the range. We can keep repeating this until the range is within a very small threshold/absolute Precision such as 0.0001.
647

748
#### Binary Search vs. Ternary Search
849
Binary search looks a zero or a specific value in case of monotonic(Non-increasing or non-decreasing) input; ternary search is used to locate an extremum for unimodal(Having a single extremum) inputs.
@@ -15,3 +56,5 @@ Binary search looks a zero or a specific value in case of monotonic(Non-increasi
1556
### More on this topic
1657
- https://en.wikipedia.org/wiki/Ternary_search
1758
- https://www.hackerearth.com/practice/algorithms/searching/ternary-search/tutorial/
59+
- http://matteolandi.blogspot.com/2012/11/ternary-search.html
60+
- http://www.thecshandbook.com/Ternary_Search
3.69 KB
Loading
Loading
Loading
3.71 KB
Loading
Loading
Loading
Loading

Searching/Ternary Search/search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ def simpleTernarySearch(item_list):
77
left, right = 0, len(item_list) - 1
88

99
found = False
10+
precision = 3
1011

1112
while left <= right:
12-
if (right - left) < 3: #Here 3 is the smallest range to divide the left and right value
13+
if (right - left) < precision: #Here 3 is the smallest range to divide the left and right value
1314
found = True
1415
break
1516

0 commit comments

Comments
 (0)