Algorithms for finding a numerical record in a list of ordered numbers

Posted by Ankur on Stack Overflow See other posts from Stack Overflow or by Ankur
Published on 2010-04-23T08:46:32Z Indexed on 2010/04/23 9:03 UTC
Read the original article Hit count: 428

Filed under:
|
|

I have a list of incomplete ordered numbers. I want to find a particular number with as few steps as possible.

Are there any improvements on this algorithm, I assume you can count the set size without difficulty - it will be stored and updated every time a new item is added.

Your object is to get your cursor over the value x

The first number (smallest) is s, and the last number (greatest) is g.

  1. Take the midpoint m1 of the set: calculate is x < m1,
  2. If yes then s <= x < m1
  3. If no then m1 < x <= g
  4. If m1 = x then you're done.

Keep repeating till you find x. Basically dividing the set into two parts with each iteration till you hit x.

The purpose is to retrieve a numerical id from a very large table to then find the associated other records.

I would imagine this is the most trivial kind of indexing available, are there improvements?

© Stack Overflow or respective owner

Related posts about database

Related posts about indexing