Repository navigation
Expand file tree
/
Copy pathrit_search.py
More file actions
84 lines (77 loc) · 2.39 KB
/
Copy pathrit_search.py
File metadata and controls
84 lines (77 loc) · 2.39 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
81
82
83
84
"""
CSCI-603: Wk 7 Sorting
Author: Zack Butler @ RIT CS
Implementations of linear and binary search
"""
def search(data, val):
"""
A linear search for a value in a list.
:param data: list of data
:param val: the value to search for
:return: True if present, False otherwise
"""
for item in data:
if item == val:
return True
return False
def where(data,val):
"""
A linear search for a value in a list
:param data: list of data
:param val: the value to search for
:return: The index of the first occurrence of the value, if present,
-1 otherwise.
"""
for ind in range(len(data)):
if data[ind] == val:
return ind
return -1
def _binSearchRec(data,val,left,right):
"""
The recursive binary search function.
:param data: list of data
:param val: the value to search for
:param left: the starting index in data
:param right: the ending index in data
:return: The index of the first occurrence of the value, if present,
-1 otherwise.
"""
if left > right:
return -1
midindex = (left+right)//2
if data[midindex] == val:
return midindex
if data[midindex] > val:
return _binSearchRec(data,val,left,midindex-1)
else:
return _binSearchRec(data,val,midindex+1,right)
def binSearch(data,val):
"""
The main binary search function, which calls the recursive helper
function to do the actual search.
:param data: A list of data
:param val: The value to search for
:return: The index of the first occurrence of the value, if present,
-1 otherwise.
"""
return _binSearchRec(data,val,0,len(data)-1)
def main():
"""
The main function.
:return: None
"""
nums = [4, 7, 2, 1, 8, 3, 6]
print("5?", search(nums,5))
print("1?", search(nums,1))
print("5?", where(nums,5))
print("1?", where(nums,1))
# binary search on unsorted data
print("binary 1?", _binSearchRec(nums,1,0,len(nums)-1))
# using the helper function
print("binary 1?", binSearch(nums,1)) # why does this work?
print("binary 2?", binSearch(nums,2))
sortednums = [2, 4, 5, 6, 8, 11, 12, 14]
print("binary 2?", binSearch(sortednums,2))
print("binary 7?", binSearch(sortednums,7))
if __name__ == '__main__':
main()