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.

Nearly All Binary Searches and Mergesorts Are Broken (2006)

98 pointsby rargulatialmost 8 years ago

14 comments

dangalmost 8 years ago
Previous discussions at <a href="https:&#x2F;&#x2F;hn.algolia.com&#x2F;?query=Nearly%20All%20Binary%20Searches%20and%20Mergesorts%20Are%20Broken&amp;sort=byDate&amp;dateRange=all&amp;type=story&amp;storyText=false&amp;prefix&amp;page=0" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?query=Nearly%20All%20Binary%20Search...</a>
maxtonalmost 8 years ago
That line from the C&#x2F;C++ &quot;fix&quot; is an atrocity; `low`, `mid`, and `high` should never have been declared as signed integers in the first place, since array indices are never negative. It&#x27;s unfortunate that in Java there is no other option than to use signed ints.
评论 #14906790 未加载
评论 #14906718 未加载
评论 #14906672 未加载
CamperBob2almost 8 years ago
Programmer: &quot;The bug is in the choice of data type. Use unsigned ints to index arrays.&quot;<p>Computer scientist: &quot;The bug is in the language. Signed integer overflow behavior should have been defined in such a way as to guarantee correct functionality in cases such as this.&quot;<p>Me: &quot;Use int64s for this sort of thing. It&#x27;s still broken, but I&#x27;ll be retired or dead before anyone notices.&quot;<p>Engineer: &quot;The bug is in the documentation. The program is correct but should have been specified for use with element counts no greater than INT_MAX &#x2F; 2.&quot;<p>Mathematician: &quot;A solution exists.&quot;<p>Manager: &quot;Ship it.&quot;
评论 #14912256 未加载
e12ealmost 8 years ago
I&#x27;m surprised about the suggested solution for java:<p><pre><code> int mid = (low + high) &gt;&gt;&gt; 1; </code></pre> I suppose things like this is why the &quot;&gt;&gt;&gt;&quot; (unsigned shift) operator exists - but it&#x27;s a bit odd when the value it works on is considered signed by the language, and implemented as two-compliment signed in memory.<p>What&#x27;s interesting to me is that this allows the sum to overflow, and would fail with the &quot;&gt;&gt;&quot; operator as far as I can tell (signed shift) - just like the original code would fail with simply dividing by 2.<p>Guess it shows java&#x27;s &quot;system language&quot; roots - in that one might expect there to be a way to be alerted to overflow when working with signed integers - but the solution here is to use a special operator to &quot;fix&quot; the problem.<p>Maybe it&#x27;s just me, but it&#x27;s a solution that would feel more at home in assembler, than I personally think it does in java.
评论 #14907288 未加载
pishpashalmost 8 years ago
So what &#x27;bout this? It&#x27;s the latest glibc.<p><a href="https:&#x2F;&#x2F;fossies.org&#x2F;dox&#x2F;glibc-2.25&#x2F;stdlib-bsearch_8h_source.html" rel="nofollow">https:&#x2F;&#x2F;fossies.org&#x2F;dox&#x2F;glibc-2.25&#x2F;stdlib-bsearch_8h_source....</a>
评论 #14907311 未加载
tzsalmost 8 years ago
&gt; int mid = low + ((high - low) &#x2F; 2);<p>That may fix things in the case of searching a sorted array, but binary search can be used more generally than that. I think that fix might not work for some of the more general applications of binary search.<p>For instance, suppose f(n) is an increasing function from the signed integers to the signed integers, with f(a) &lt; 0 and f(b) &gt; 0, and you want to find an n in (a,b), if such n exists, such that f(n) = 0. Binary search on [a, b] is a reasonable approach.<p>If a &lt; 0 and b &gt; 0, then that mid computation could overflow on the subtraction.
评论 #14908117 未加载
评论 #14907169 未加载
评论 #14907177 未加载
jtchangalmost 8 years ago
I don&#x27;t think that is broken. The algorithm is sound conceptually. I was almost looking for some type of theory where all mergesorts and binary searches could be improved.
评论 #14907206 未加载
slolean13almost 8 years ago
(High-low) works, well if we consider this from a pointer pov!
pagadealmost 8 years ago
Won&#x27;t this line have similar issue?<p><pre><code> return -(low + 1); &#x2F;&#x2F; key not found.</code></pre>
KirinDavealmost 8 years ago
A more modern version of this is: nearly all discussion about sorts that claims computers can&#x27;t do search in better than O(n log n) are wrong and have been for some time.<p>Edit: I&#x27;m quite surprised I&#x27;m being modded down given the magnitude of what I&#x27;m implying. I suspect someone doesn&#x27;t understand what I&#x27;m saying.
评论 #14907158 未加载
sriram_iyengaralmost 8 years ago
Classic programming defect !
sillysaurus3almost 8 years ago
I wish it was worth a lot to be good at systems programming these days. It seems like the huge salaries are for rails senior devs. I spent like ten years getting really good at this stuff (the (low + high)&#x2F;2 line jumped right out at me) but nowadays it feels like being really good at trivial pursuit.<p>It&#x27;s interesting how much things have changed in the last decade. I wonder what next decade&#x27;s &quot;Rails&quot; will be? Could it be possible to predict?
评论 #14906824 未加载
评论 #14906988 未加载
评论 #14907033 未加载
评论 #14907250 未加载
评论 #14911473 未加载
评论 #14906715 未加载
ScottBursonalmost 8 years ago
The bug isn&#x27;t in your algorithm; the bug is in your language, which doesn&#x27;t provide arbitrary-precision integer arithmetic by default.
评论 #14906968 未加载
评论 #14906881 未加载
评论 #14907249 未加载
评论 #14912287 未加载
评论 #14906802 未加载
评论 #14907120 未加载
jmullalmost 8 years ago
* for arrays with counts over a billion if you choose to use signed 32-bit integers for array indexes.<p>Not that it isn&#x27;t a potential problem, but it&#x27;s a narrow issue.