@@ -164,6 +164,20 @@ def _rank(key: int, the_array: [int], min_index: int, max_index: int) -> int:
164
164
# ---------------------------------------------------------------------------------------------------------
165
165
166
166
167
+ def brute_fore_search (key : int , the_array : [int ], index : int ) -> int :
168
+ """Returns the index of the key if present, otherwise -1 using brute force search"""
169
+ if index == len (the_array ):
170
+ return - 1
171
+
172
+ if the_array [index ] == key :
173
+ return index
174
+
175
+ return brute_fore_search (key , the_array , index + 1 )
176
+
177
+
178
+ # ---------------------------------------------------------------------------------------------------------
179
+
180
+
167
181
def main ():
168
182
"""Testing"""
169
183
test_x : float = 0.1
@@ -239,6 +253,10 @@ def main():
239
253
print ("- Binary Search: key = 4" )
240
254
print (f"index: { binary_search (4 , test_int_array ) } " )
241
255
256
+ print ()
257
+ print ("- Brute Force Search: key = 8" )
258
+ print (f"index: { brute_fore_search (8 , sorted (test_int_array ), 0 ) } " )
259
+
242
260
243
261
# ---------------------------------------------------------------------------------------------------------
244
262
0 commit comments