-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathdouble_linear_search.py
More file actions
80 lines (60 loc) · 2.11 KB
/
double_linear_search.py
File metadata and controls
80 lines (60 loc) · 2.11 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import annotations
def double_linear_search(array: list[int], search_item: int) -> int:
"""
Iterate through the array from both sides to find the index of search_item.
This algorithm searches for a target value from both the start and end of the
array simultaneously, converging toward the middle. It returns the index of the
first match found (prioritizing from the start when both ends match simultaneously).
:param array: the array to be searched
:param search_item: the item to be searched
:return the index of search_item, if search_item is in array, else -1
Examples:
>>> double_linear_search([1, 5, 5, 10], 1)
0
>>> double_linear_search([1, 5, 5, 10], 5)
1
>>> double_linear_search([1, 5, 5, 10], 100)
-1
>>> double_linear_search([1, 5, 5, 10], 10)
3
>>> double_linear_search([], 1)
-1
>>> double_linear_search([42], 42)
0
>>> double_linear_search([42], 99)
-1
>>> double_linear_search([1, 2, 3, 4, 5], 3)
2
>>> double_linear_search([1, 2, 3, 4, 5], 1)
0
>>> double_linear_search([1, 2, 3, 4, 5], 5)
4
>>> double_linear_search([10, 10, 10, 10], 10)
0
>>> double_linear_search([-5, -2, 0, 3, 7], -5)
0
>>> double_linear_search([-5, -2, 0, 3, 7], 0)
2
>>> double_linear_search([-5, -2, 0, 3, 7], 7)
4
>>> double_linear_search([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
4
>>> double_linear_search([1, 2, 3, 4, 5, 6, 7, 8, 9], 9)
8
>>> double_linear_search([100, 200, 300, 400], 250)
-1
"""
# define the start and end index of the given array
start_ind, end_ind = 0, len(array) - 1
while start_ind <= end_ind:
if array[start_ind] == search_item:
return start_ind
elif array[end_ind] == search_item:
return end_ind
else:
start_ind += 1
end_ind -= 1
# returns -1 if search_item is not found in array
return -1
if __name__ == "__main__":
print(double_linear_search(list(range(100)), 40))