Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfe442c

Browse files
committed
FEAT Implemented recursive brute force search algorithm
1 parent 36bab00 commit cfe442c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ def _rank(key: int, the_array: [int], min_index: int, max_index: int) -> int:
164164
# ---------------------------------------------------------------------------------------------------------
165165

166166

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+
167181
def main():
168182
"""Testing"""
169183
test_x: float = 0.1
@@ -239,6 +253,10 @@ def main():
239253
print("- Binary Search: key = 4")
240254
print(f"index: { binary_search(4, test_int_array) }")
241255

256+
print()
257+
print("- Brute Force Search: key = 8")
258+
print(f"index: { brute_fore_search(8, sorted(test_int_array), 0) }")
259+
242260

243261
# ---------------------------------------------------------------------------------------------------------
244262

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /