forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathternary_search.py
More file actions
53 lines (41 loc) · 1.21 KB
/
ternary_search.py
File metadata and controls
53 lines (41 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Author: OMKAR PATHAK
Created at: 26th August 2017
Time complexity: O(logn)
More Info: https://en.wikipedia.org/wiki/Ternary_search
'''
from __future__ import division
import inspect
def search(_list, left, right, target):
if right >= left:
mid1 = (left + right) // 3
mid2 = (mid1 + right) // 3
# if target is present at mid1
if _list[mid1] == target:
return mid1
# if target is present at mid2
if _list[mid2] == target:
return mid2
# if target is present at left one-third
if _list[mid1] > target:
return search(_list, left, mid1 - 1, target)
# if target is present at right one-third
if _list[mid2] < target:
return search(_list, mid2 + 1, right, target)
# if target is present in the middle one-third
return search(_list, mid1 + 1, mid2 - 1, target)
return False
def time_complexities():
"""
Return information on functions
time complexity
:return: string
"""
return "Time complexity: O(logn)"
def get_code():
"""
easily retrieve the source code
of the function
:return: source code
"""
return inspect.getsource(search)