Find next lower item in a sorted list

Posted by Sebastian on Stack Overflow See other posts from Stack Overflow or by Sebastian
Published on 2010-04-07T09:10:33Z Indexed on 2010/04/07 9:13 UTC
Read the original article Hit count: 242

Filed under:

Hey guys,

let's say I have a sorted list of Floats. Now I'd like to get the index of the next lower item of a given value. The usual for-loop aprroach has a complexity of O(n). Since the list is sorted there must be a way to get the index with O(log n).

My O(n) approach:

index=0
for i,value in enumerate(mylist):
    if value>compareValue:
        index=i-1

Is there a datatype for solving that problem in O(log n)?

best regards Sebastian

© Stack Overflow or respective owner

Related posts about python