TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Eytzinger Binary Search

118 pointsby mikecarltonabout 2 years ago

7 comments

serejaabout 2 years ago
Author here. There is a newer and significantly expanded version of the article: <a href="https:&#x2F;&#x2F;en.algorithmica.org&#x2F;hpc&#x2F;data-structures&#x2F;binary-search&#x2F;" rel="nofollow">https:&#x2F;&#x2F;en.algorithmica.org&#x2F;hpc&#x2F;data-structures&#x2F;binary-searc...</a>
评论 #35751273 未加载
评论 #35756782 未加载
评论 #35757125 未加载
评论 #35751341 未加载
utopcellabout 2 years ago
There exists a 2022 CPPcon presentation covering efficient binary searching, which includes Eytzinger layouts [1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;CppCon&#x2F;CppCon2022&#x2F;blob&#x2F;main&#x2F;Presentations&#x2F;binary-search-cppcon.pdf">https:&#x2F;&#x2F;github.com&#x2F;CppCon&#x2F;CppCon2022&#x2F;blob&#x2F;main&#x2F;Presentations...</a>
评论 #35753165 未加载
kalimanzaroabout 2 years ago
Relevant recent HN thread: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35737862" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35737862</a><p>(Beautiful branchless binary search)
toolsliveabout 2 years ago
very nice. small remark: iirc, there&#x27;s a risk of overflow if you naively do `m = (l + r) &#x2F;2`
einpoklumabout 2 years ago
tl;dr:<p>1. Avoid branching by always reducing the search range from n to n-n&#x2F;2 (never n&#x2F;2).<p>2. Preprocess the input (a sorted array) to Eytzinger order: Middle element, then first and third quartile elements etc. Now, if your search checks an element at position k, it will next want to look at either position 2k or 2k+1.<p>3. Some minor tweaks like aligned storage and rounding up size to a power of 2.
jqpabc123about 2 years ago
A good alternative to binary search on modern hardware that should not be overlooked is indexed sequential search.<p><a href="https:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;indexed-sequential-search&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;indexed-sequential-search&#x2F;</a>
评论 #35758168 未加载
评论 #35753056 未加载
utopcellabout 2 years ago
Eytzinger layouts are great, but they require more space if one is not allowed to touch the original sorted array. Then again, if you are allowed to use extra space, binary search can be implemented in worst-case constant time for integer arrays.