can you write a binary search, given a data array of key-value pairs sorted by key, and a query key? also, assuming that the query keys come from the same distribution, can you make your binary search adaptive to the history of queries, to improve its performance?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Let's start with a basic binary search algorithm for an array of key-value pairs sorted by key.
Basic Binary Search
python
def binary_search(data, query_key):
left, right = 0, len(data) - 1
while left <= right:
mid = (left + right) // 2
key, value = data[mid]
if key == query_key:
return value
elif key < query_key:
left = mid + 1
else:
right = mid - 1
return None # Return None if the key is not found
# Example usage
data = [(1, 'a'), (3, 'b'), (5, 'c'), (7, 'd'), (9, 'e')]
query_key = 5
result = binary_search(data, query_key)
print(result) # Output: 'c'
Adaptive Binary Search
To make the binary search adaptive based on the history of queries, we can keep track of the frequency of query keys and adjust the search pivot accordingly. This can be done by maintaining a weighted pivot that favors more frequently queried sections of the data.
Here's a simple implementation of an adaptive binary search:
python
import bisect
class AdaptiveBinarySearch:
def __init__(self, data):
self.data = data
self.keys = [key for key, value in data]
self.query_counts = [0] * len(data)
def search(self, query_key):
left, right = 0, len(self.data) - 1
while left <= right:
mid = self._weighted_mid(left, right)
key, value = self.data[mid]
if key == query_key:
self.query_counts[mid] += 1
return value
elif key < query_key:
left = mid + 1
else:
right = mid - 1
return None # Return None if the key is not found
def _weighted_mid(self, left, right):
total_weight = sum(self.query_counts[left:right + 1])
if total_weight == 0:
return (left + right) // 2
cumulative_weight = 0
for i in range(left, right + 1):
cumulative_weight += self.query_counts[i]
if cumulative_weight >= total_weight / 2:
return i
return (left + right) // 2
# Example usage
data = [(1, 'a'), (3, 'b'), (5, 'c'), (7, 'd'), (9, 'e')]
adaptive_search = AdaptiveBinarySearch(data)
queries = [5, 3, 5, 7, 5, 1, 5]
for query in queries:
result = adaptive_search.search(query)
print(f"Query {query}: {result}")
Explanation
Basic Binary Search:
The binary_search function performs a standard binary search on the sorted key-value pairs.
Adaptive Binary Search:
The AdaptiveBinarySearch class maintains the data and a list of query counts (query_counts) to track the frequency of queries for each key.
The search method performs a binary search but uses the _weighted_mid method to determine the pivot point based on the query frequencies.
The _weighted_mid method calculates a weighted midpoint that favors more frequently queried sections of the data. If no queries have been made yet, it defaults to the standard midpoint.
This adaptive approach can improve performance over time as the search algorithm learns the distribution of the query keys.
“can you write a binary search, given a data array of key-value pairs sorted by key, and….” Conversation with mistral-large-2407, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850gqhs196hr4cqsrgrnwfn