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 6368391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:40:09+00:00 2026-05-25T00:40:09+00:00

In writing some code today, I have happened upon a circumstance that has caused

  • 0

In writing some code today, I have happened upon a circumstance that has caused me to write a binary search of a kind I have never seen before. Does this binary search have a name, and is it really a "binary" search?

Motivation

First of all, in order to make the search easier to understand, I will explain the use case that spawned its creation.

Say you have a list of ordered numbers. You are asked to find the index of the number in the list that is closest to x.

int findIndexClosestTo(int x);

The calls to findIndexClosestTo() always follow this rule:

If the last result of findIndexClosestTo() was i, then indices closer to i have greater probability of being the result of the current call to findIndexClosestTo().

In other words, the index we need to find this time is more likely to be closer to the last one we found than further from it.

For an example, imagine a simulated boy that walks left and right on the screen. If we are often querying the index of the boy’s location, it is likely he is somewhere near the last place we found him.

Algorithm

Given the case above, we know the last result of findIndexClosestTo() was i (if this is actually the first time the function has been called, i defaults to the middle index of the list, for simplicity, although a separate binary search to find the result of the first call would actually be faster), and the function has been called again. Given the new number x, we follow this algorithm to find its index:

  1. interval = 1;
  2. Is the number we’re looking for, x, positioned at i? If so, return i;
  3. If not, determine whether x is above or below i. (Remember, the list is sorted.)
  4. Move interval indices in the direction of x.
  5. If we have found x at our new location, return that location.
  6. Double interval. (i.e. interval *= 2)
  7. If we have passed x, go back interval indices, set interval = 1, go to 4.

Given the probability rule stated above (under the Motivation header), this appears to me to be the most efficient way to find the correct index. Do you know of a faster way?

  • 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-25T00:40:09+00:00Added an answer on May 25, 2026 at 12:40 am

    What you are doing is (IMHO) a version of Interpolation search

    In a interpolation search you assume numbers are equally distributed, and then you try to guess the location of a number from first and last number and length of the array.

    In your case, you are modifying the interpolation-algo such that you assume the Key is very close to the last number you searched.

    Also note that your algo is similar to algo where TCP tries to find the optimal packet size. (dont remember the name 🙁 )

    1. Start slow
      1. Double the interval
      2. if Packet fails restart from the last succeeded packet./ Restart
        from default packet size.. 3.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I was writing some code today that basically looks like this: string returnString
I'm writing some code that id like to be able to work with any
I'm writing some code that handles logging xml data and I would like to
I'm writing some code that uses dynamic shared libraries as plugins. My command line
I was writing some code today and something was not working as I expected.
I'm writing some code to acquire images that live on a server. The image
I am writing some code to determine whether a network domain is registered. For
I am writing some code and trying to speed it up using SIMD intrinsics
I'm writing some code for a class constructor which loops through all the properties
I'm writing some code in python and I'm having trouble when trying to retrieve

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.