MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / binary_search_with_duplicates

Function binary_search_with_duplicates

searches/binary_search.py:245–317  ·  view source on GitHub ↗

Pure implementation of a binary search algorithm in Python that supports duplicates. Resources used: https://stackoverflow.com/questions/13197552/using-binary-search-with-sorted-array-with-duplicates The collection must be sorted in ascending order; otherwise the result will be

(sorted_collection: list[int], item: int)

Source from the content-addressed store, hash-verified

243
244
245def binary_search_with_duplicates(sorted_collection: list[int], item: int) -> list[int]:
246 """Pure implementation of a binary search algorithm in Python that supports
247 duplicates.
248
249 Resources used:
250 https://stackoverflow.com/questions/13197552/using-binary-search-with-sorted-array-with-duplicates
251
252 The collection must be sorted in ascending order; otherwise the result will be
253 unpredictable. If the target appears multiple times, this function returns a
254 list of all indexes where the target occurs. If the target is not found,
255 this function returns an empty list.
256
257 :param sorted_collection: some ascending sorted collection with comparable items
258 :param item: item value to search for
259 :return: a list of indexes where the item is found (empty list if not found)
260
261 Examples:
262 >>> binary_search_with_duplicates([0, 5, 7, 10, 15], 0)
263 [0]
264 >>> binary_search_with_duplicates([0, 5, 7, 10, 15], 15)
265 [4]
266 >>> binary_search_with_duplicates([1, 2, 2, 2, 3], 2)
267 [1, 2, 3]
268 >>> binary_search_with_duplicates([1, 2, 2, 2, 3], 4)
269 []
270 """
271 if list(sorted_collection) != sorted(sorted_collection):
272 raise ValueError("sorted_collection must be sorted in ascending order")
273
274 def lower_bound(sorted_collection: list[int], item: int) -> int:
275 """
276 Returns the index of the first element greater than or equal to the item.
277
278 :param sorted_collection: The sorted list to search.
279 :param item: The item to find the lower bound for.
280 :return: The index where the item can be inserted while maintaining order.
281 """
282 left = 0
283 right = len(sorted_collection)
284 while left < right:
285 midpoint = left + (right - left) // 2
286 current_item = sorted_collection[midpoint]
287 if current_item < item:
288 left = midpoint + 1
289 else:
290 right = midpoint
291 return left
292
293 def upper_bound(sorted_collection: list[int], item: int) -> int:
294 """
295 Returns the index of the first element strictly greater than the item.
296
297 :param sorted_collection: The sorted list to search.
298 :param item: The item to find the upper bound for.
299 :return: The index where the item can be inserted after all existing instances.
300 """
301 left = 0
302 right = len(sorted_collection)

Callers

nothing calls this directly

Calls 2

lower_boundFunction · 0.85
upper_boundFunction · 0.85

Tested by

no test coverage detected