←back to thread

386 points ingve | 1 comments | | HN request time: 0s | source
Show context
2102922286 ◴[] No.35738100[source]
A cool related algorithm is https://algorithmica.org/en/eytzinger

In addition to being branchless, it also has better cache properties than a standard binary search tree.

If you're doing a binary search in an array, you start in the middle, and then jump all the way to the midway point of one half, and so on. As a result, there's a lot of distance between each read that you do in the array. Thus, each read is putting extra strain on the cache (or the reads won't be cached).

The CPU cache performs much better if the values you want to read are close to each other. Enter the Eytzinger Binary Search. The idea is that, if you're always going to be accessing the root of the tree, and then one of its two children--you should just put those children physically close to the root, so they'll all be on the same cache line!

replies(8): >>35738287 #>>35738451 #>>35738561 #>>35739155 #>>35740049 #>>35740397 #>>35740690 #>>35744101 #
zelphirkalt ◴[] No.35738561[source]
Isn't that like storing a heap inside an array, with indices and values like this:

    0: top of heap,
    1: left of top,
    2: right of top,
    3: left of left of top
    ...

?
replies(1): >>35739255 #
contravariant ◴[] No.35739255[source]
It is, but so is storing a sorted array.
replies(1): >>35739531 #
account42 ◴[] No.35739531[source]
It's still misleading to call this a binary search when that is commonly understood as an algorithm operating on a sorted range which is not just an arbitrary datastructure but something that already exists in many situations. If you just need fast lower_bound lookup for a set that you control and which has no other uses then sure this is nice, but more commonly you only care if an element is in the set and in that case if you can control the data structure then there are many more options.
replies(1): >>35739888 #
1. eska ◴[] No.35739888[source]
I agree with you.

Algorithms don’t exist in a bubble, but are always accompanied with a data structure.

A heap sorted array is different from a linearly sorted array. Binary search is only defined for the latter.