OMG! Nearly All Binary Searches and Mergesorts Are Broken

2025-01-11
OMG! Nearly All Binary Searches and Mergesorts Are Broken

Google software engineer Joshua Bloch revealed a nearly two-decade-old bug lurking in binary search algorithms, found in both the JDK and Jon Bentley's 'Programming Pearls'! The bug stems from the line `int mid = (low + high) / 2;`, causing integer overflow and array index out-of-bounds exceptions when the sum of `low` and `high` exceeds the maximum positive integer value. This bug only manifests with massive datasets, making it particularly dangerous in today's big data world. The article explores various fixes and emphasizes that bugs can persist even with rigorous testing and proofs, urging programmers to remain cautious and humble.