Compare commits
2 Commits
f01b0069a0
...
7fbfc8fd03
| Author | SHA1 | Date | |
|---|---|---|---|
| 7fbfc8fd03 | |||
| a866f5ea66 |
@@ -21,8 +21,9 @@ class Search:
|
|||||||
for _ in range(n):
|
for _ in range(n):
|
||||||
array.append(random.randint(0, 1000))
|
array.append(random.randint(0, 1000))
|
||||||
|
|
||||||
# if 42 not in data:
|
#I added this for testing purposes, to make sure 42 was in the list
|
||||||
# data[random.randint(0, n - 1)] = 42
|
if 42 not in array:
|
||||||
|
array[random.randint(0, n - 1)] = 42
|
||||||
|
|
||||||
return array
|
return array
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ class Search:
|
|||||||
for value in arr:
|
for value in arr:
|
||||||
if value == target:
|
if value == target:
|
||||||
print("Unsuccessful checks:", checks)
|
print("Unsuccessful checks:", checks)
|
||||||
|
return checks
|
||||||
checks += 1
|
checks += 1
|
||||||
|
|
||||||
print("Not found.")
|
print("Not found.")
|
||||||
@@ -98,23 +99,23 @@ def main():
|
|||||||
n = 1000
|
n = 1000
|
||||||
arr = search.generate_random_list(n) #generate the list of n random integers
|
arr = search.generate_random_list(n) #generate the list of n random integers
|
||||||
|
|
||||||
for multiplier in [0.5, 1, 2, 4]:
|
for multiplier in [0.5, 1, 2, 4]: #I used a "multiplier" to avoid rewriting the code for each query
|
||||||
query_count = int(n * multiplier)
|
query_count = int(n * multiplier)
|
||||||
queries = []
|
queries = []
|
||||||
|
|
||||||
for _ in range(query_count):
|
for _ in range(query_count):
|
||||||
queries.append(random.choice(base_array))
|
queries.append(random.choice(arr))
|
||||||
|
|
||||||
# Linear timing
|
# Linear timing
|
||||||
start = time.perf_counter()
|
start = time.perf_counter()
|
||||||
for q in queries:
|
for q in queries:
|
||||||
search.linear_search_count(base_array, q)
|
search.linear_search_count(arr, q)
|
||||||
end = time.perf_counter()
|
end = time.perf_counter()
|
||||||
linear_time = end - start
|
linear_time = end - start
|
||||||
|
|
||||||
# Sorting + Binary timing
|
# Sorting + Binary timing
|
||||||
start = time.perf_counter()
|
start = time.perf_counter()
|
||||||
sorted_arr = sorted(base_array)
|
sorted_arr = sorted(arr)
|
||||||
for q in queries:
|
for q in queries:
|
||||||
search.binary_search_recursive(sorted_arr, q, 0, len(sorted_arr) - 1)
|
search.binary_search_recursive(sorted_arr, q, 0, len(sorted_arr) - 1)
|
||||||
end = time.perf_counter()
|
end = time.perf_counter()
|
||||||
|
|||||||
Reference in New Issue
Block a user