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.
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.
Standard Binary Search
Here is a Python implementation of a standard binary search:
python
def binary_search(data, query_key):
"""
Standard binary search algorithm.
Args:
data (list): A list of key-value pairs sorted by key.
query_key (int): The key to search for.
Returns:
int: The index of the key-value pair if found, -1 otherwise.
"""
low = 0
high = len(data) - 1
while low <= high:
mid = (low + high) // 2
if data[mid][0] == query_key:
return mid
elif data[mid][0] < query_key:
low = mid + 1
else:
high = mid - 1
return -1
Adaptive Binary Search
To make the binary search adaptive to the history of queries, we can use a variant called Interpolation Search. Interpolation search is an improvement over binary search where the values in a sorted array are uniformly distributed. Instead of always going to the middle element, interpolation search estimates the position of the target value, taking into account the values at the bounds of the search interval and the value being searched.
However, if the distribution of query keys is not uniform, Interpolation Search might not perform well. In this case, we can use a Self-Organizing Binary Search. This algorithm moves the recently accessed elements to the front of the array, so that the next time they are accessed, they can be found more quickly.
Here is a Python implementation of a Self-Organizing Binary Search:
python
class AdaptiveBinarySearch:
def __init__(self, data):
"""
Initialize the adaptive binary search.
Args:
data (list): A list of key-value pairs sorted by key.
"""
self.data = data
def search(self, query_key):
"""
Search for a key in the data.
Args:
query_key (int): The key to search for.
Returns:
int: The index of the key-value pair if found, -1 otherwise.
"""
low = 0
high = len(self.data) - 1
while low <= high:
mid = (low + high) // 2
if self.data[mid][0] == query_key:
# Move the accessed element to the front
self.data.insert(0, self.data.pop(mid))
return 0
elif self.data[mid][0] < query_key:
low = mid + 1
else:
high = mid - 1
return -1
In this example, the adaptive binary search moves the recently accessed elements to the front of the array, so that the next time they are accessed, they can be found more quickly.
“can you write a binary search, given a data array of key-value pairs sorted by key, and….” Conversation with llama-3.1-70b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850akdagt8pm2d4jar6796b