简体   繁体   中英

Efficiently find index of smallest number larger than some value in a large sorted list

If I have a long list of sorted numbers, and I want to find the index of the smallest element larger than some value, is there a way to implement it more efficiently than using binary search on the entire list?

For example:

import random
c = 0
x = [0 for x in range(50000)]

for n in range(50000):
    c += random.randint(1,100)
    x[n] = c

What would be the most efficient way of finding the location of the largest element in x smaller than some number, z

I know that you can already do:

import bisect
idx = bisect.bisect(x, z)

But assuming that this would be performed many times, would there be an even more efficient way than binary search? Since the range of the list is large, creating a dict of all possible integers uses too much memory. Would it be possible to create a smaller list of say every 5000 numbers and use that to speed up the lookup to a specific portion of the large list?

Can you try if this can be a solution? It takes long to generate the list, but seems fast to report the result.

Given the list:

import random
limit = 50 # to set the number of elements
c = 0
x = [0 for x in range(limit)]

for n in range(limit):
    c += random.randint(1,100)
    x[n] = c
print(x)

Since it is a sorted list, you could retrieve the value using a for loop:

z = 1600 # reference for lookup

res = ()
for i, n in enumerate(x):
  if n > z:
    res = (i, n)
    break

print (res) # this is the index and value of the elements that match the condition to break
print(x[res[0]-1]) # this is the element just before

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM