16 lines
495 B
Python
16 lines
495 B
Python
def choose_search_strategy(n, q):
|
|
cost_linear = q * n
|
|
cost_binary = n * (n.bit_length()) + q * (n.bit_length())
|
|
cost_hash = n + q
|
|
if cost_linear <= cost_binary and cost_linear <= cost_hash:
|
|
return "linear"
|
|
if cost_binary <= cost_hash:
|
|
return "binary_search_with_sort"
|
|
return "hash_table"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(choose_search_strategy(10**3, 1))
|
|
print(choose_search_strategy(10**5, 100))
|
|
print(choose_search_strategy(10**6, 10**6))
|