Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7127959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:05:16+00:00 2026-05-28T11:05:16+00:00

I have a large pool of objects with starting number and ending number. For

  • 0

I have a large pool of objects with starting number and ending number. For example:

(999, 2333, data) 
(0, 128, data) 
(235, 865, data)
...

Assuming that the intervals don’t overlap with each other. And I am writing a function that takes a number and locate the object that (low, high) contains it. Say given 333, I want the 3rd objects on the list.

Is there any way I can do this as efficiently as possible, short of linear search? I was thinking about binary search, but having some difficulties of coping with the range check.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:05:17+00:00Added an answer on May 28, 2026 at 11:05 am

    First of all, it is not at all clear that binary search is warranted here. It may well be that linear search is faster when the number of intervals is small.

    If you’re concerned about performance, the prudent thing to do is to profile the code, and perhaps benchmark both methods on your typical inputs.

    Disclaimers aside, binary search could be implemented by sorting the intervals once, and then repeatedly using the bisect module to do the search:

    import bisect
    
    intervals = [(999, 2333, 'int1'), (0, 128, 'int2'), (235, 865, 'int3')]
    intervals.sort()
    
    def find_int(intervals, val):
       pos = bisect.bisect_left([interval[1] for interval in intervals], val)
       if pos < len(intervals) and val >= intervals[pos][0]:
          return intervals[pos]
       else:
          return None
    
    print(find_int(intervals, 0))
    print(find_int(intervals, 1))
    print(find_int(intervals, 200))
    print(find_int(intervals, 998))
    print(find_int(intervals, 999))
    print(find_int(intervals, 1000))
    print(find_int(intervals, 2333))
    print(find_int(intervals, 2334))
    

    In the above, I assume that the intervals are non-overlapping, and that the interval includes both its start and end points.

    Lastly, to improve performance, one might consider factoring [interval[1] for interval in intervals] out of the function and doing it just once at the start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have large set will a number of variables: set.seed (14) pool = sample
Interpolating Large Datasets I have a large data set of about 0.5million records representing
Basically, I have a large number of C structs to keep track of, that
I have a large (about 10 GB with a 20 GB innodb buffer pool)
I have a large number of projects on the go, and several solutions (which
Our application has a very large, wide table of transactional data. We have a
I have a WCF service handling a very large number of requests (thousands per
Let's say I have a large, between 50 and 200, pool of individual functions
I have a local SQLite Database and need to return a large number of
I have a number of large files with many thousands of lines in python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.