-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearches.py
88 lines (57 loc) · 2.24 KB
/
Searches.py
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
85
86
87
88
"""
This module will hold all of the sorting algorithms.
Student Name: Michael Fourie
Student Number: 2102203
"""
def binarySearch(aList, target):
"""
This is the binary search function.
Input: list of values, target value
Output: tuple containing the index of the search vale, none if not found, aswell as the count of comparisons the function made
"""
low = 0
high = len(aList) - 1
# This will count the number of itterations
itterationCount = 0
while high >= low:
# This will add a value to the itteration count
itterationCount += 1
midpoint = (high + low) // 2
if target == aList[midpoint]:
# Tuple to be returned from function
returnedTuple = (midpoint, itterationCount)
return returnedTuple
elif target > aList[midpoint]:
low = midpoint + 1
elif target < aList[midpoint]:
high = midpoint - 1
# Tuple to be returned from function
returnedTuple = (None, itterationCount)
return returnedTuple
def linearSearch(aList, target):
"""
This is the linear search function.
Input: a list of values, as well as target value
Output: Tuple of index of searched value, none if not found, aswell as the number of comparisons made
"""
# Count the number of itterations
itterationCount = 0
for element in range(len(aList)):
if aList[element] == target:
# The tutple to be returned when element is found in the list
returnedTuple = (element, itterationCount)
return returnedTuple
else:
# Increase itteration count by one when element not found
itterationCount += 1
# When element not found
returnedTuple = (None, itterationCount)
return returnedTuple
if __name__ == '__main__':
aList = [1, 3, 5, 7, 9, 11, 13, 15, 18, 21]
target = 16
print(binarySearch(aList, target), 'should be None and three')
print(linearSearch(aList, target), 'should be None and 10')
target = 15
print(binarySearch(aList, target), 'should be 7 and 2')
print(linearSearch(aList, target), 'should be 7 and 7')