I have a sorted list of numbers and I need to get it return the range of index that the number appears. My list is:
daysSick = [0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 11, 15, 24]
If I searched 0, I need to return (0, 3). Right now I can only get it to find the spot of one number! I know how to do the binary search, but I am stuck how to make it move up and down from the position to find the other same values!
low = 0
high = len(daysSick) - 1
while low <= high :
mid = (low + high) // 2
if value < daysSick[mid]:
high = mid - 1
elif value > list[mid]:
low = mid + 1
else:
return mid
I present a solution faster than the raw functions taken from the
bisect
librarySolution
With Optimised Binary Search
Comparision vs.
Bisect
Using customised binary search...
Copying the raw code from the source from
bisect
into python...Using default import is by far the fastest as I think it might be optimised behind the scenes ...
Extra
Without ext. libraries but not binary search