Search code examples
performancecontainersbinary-searchcpu-cache

Binary search with looking nearby values


I am trying to find, if someone implemented binary search in following way -

Let suppose we have array of some elements, placed in contiguous memory.

Then when you compare middle element, the next few elements should be already in the the CPU cache. Comparing should be already free?

Yet I can not find anyone who doing this.

If no one do that, what could be the reason?


Solution

  • Classic binary search can be thought of as defining a binary tree structure on the elements. For example, if your array has 15 elements numbered 1 through 15, you would start out by looking at the middle element "8", and from there go either left or right to element "4" or "12":

    in-order layout for a binary tree

    (from Brodal et al, "Cache oblivious search trees via binary trees of small height", SODA'02, PDF preprint link)

    What you are proposing is essentially adding a few more elements into each node, so that the "8" element would also contain some adjacent elements e.g. "9", "10", "11". I don't think this has been studied extensively before, but there is another very related idea that has been studied, namely going from a binary tree (with two children on each node) to a B-ary tree ("B-tree", with B children on each node). This is where you have multiple elements in a node that split up the resulting data into many different ranges.

    By comparing a search key to the B-1 different elements in a node, you can determine which of the B children to recurse into.

    B-tree example

    It's possible to rearrange the elements in an array so that this search structure can be realized without using pointers and multiple memory allocations, so that it becomes just as space-efficient as doing binary search in a regular sorted array, but with fewer "jumps" when doing the search. In a regular sorted array, binary search does roughly log_2(N) jumps, whereas search in a B-tree does only log_2(N) / log_2(B) = log_B(N) jumps, which is a factor log_2(B) fewer!

    Sergey Slotin has written a blog post with a complete example of how to implement a static B-tree that is stored implicitly in an array: https://algorithmica.org/en/b-tree