Skip to content

bisection_timestamp_search returns the index closest and SMALLER than the query #100

Open
@georgegu1997

Description

@georgegu1997

The bisection search used in this line returns the index closest and smaller than the query rather than the one closest to it.

See example below

timed_data = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
query_timestamp_ns = 2.9

# If this is safe we perform the Bisection search
start = 0
end = len(timed_data) - 1
while start < end:
    mid = (start + end) // 2
    mid_timestamp = timed_data[mid]
    if mid_timestamp == query_timestamp_ns:
        start = mid
        break
    if mid_timestamp < query_timestamp_ns:
        start = mid + 1
    else:
        end = mid - 1
        
print(start, timed_data[start])  # it prints 0 1
timed_data = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
query_timestamp_ns = 1.1

# If this is safe we perform the Bisection search
start = 0
end = len(timed_data) - 1
while start < end:
    mid = (start + end) // 2
    mid_timestamp = timed_data[mid]
    if mid_timestamp == query_timestamp_ns:
        start = mid
        break
    if mid_timestamp < query_timestamp_ns:
        start = mid + 1
    else:
        end = mid - 1
        
print(start, timed_data[start]) # This also prints 0 1

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions