Compare commits
2 Commits
7f9e88d7b6
...
1d5e02949a
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d5e02949a | |||
| d9414d044c |
@@ -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,8 +28,44 @@ 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
|
||||||
|
|||||||
Reference in New Issue
Block a user