Compare commits

...

2 Commits

Author SHA1 Message Date
1d5e02949a added recursive binary search 2026-03-03 18:09:02 -05:00
d9414d044c added linear_search_print 2026-03-03 18:07:32 -05:00

View File

@@ -12,7 +12,7 @@ from __future__ import annotations
import random #to generate the 100 random values import random #to generate the 100 random values
import time #for part 4, to calculae total time import time #for part 4, to calculae total time
#### Write your class here
class Search: class Search:
""" """
@@ -28,10 +28,46 @@ class Search:
return data return data
"""
This funciton preforms a search on the list for the target value, and prints the number of checks it took to find the target.
"""
def linear_search_print(self, arr, target):
checks = 0
for value in arr:
if value == target:
print("Unsuccessful checks:", checks)
return checks
checks += 1
print("Not found.")
return checks
"""
This function pre
"""
def linear_search_count(self, arr, target):
checks = 0
for value in arr:
checks += 1
if value == target:
return checks
return checks
def binary_search_recursive(self, arr, target, low, high):
if low > high:
return -1
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
return self.binary_search_recursive(arr, target, mid + 1, high)
else:
return self.binary_search_recursive(arr, target, low, mid - 1)
### write your tests in main function ### write your tests in main function
def main(): def main():
pass pass