René's URL Explorer Experiment


Title: Array Searching in Java: binarySearch() on Unsorted Arrays

Open Graph Title: Array Searching in Java: binarySearch() on Unsorted Arrays

Description: binarySearch returns -(insertion point)-1, not -1. On unsorted arrays it returns wrong indices silently. How this corrupted patient records.

Open Graph Description: binarySearch returns -(insertion point)-1, not -1. On unsorted arrays it returns wrong indices silently. How this corrupted patient records.

Opengraph URL: https://thecodeforge.io/java/array-searching-java/

direct link

Domain: thecodeforge.io


Hey, it has json ld scripts:
{
  "@context": "https://schema.org",
  "@type": "TechArticle",
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://thecodeforge.io/java/array-searching-java/"
  },
  "headline": "binarySearch() on Unsorted Arrays — Silent Index Corruption",
  "description": "binarySearch returns -(insertion point)-1, not -1. On unsorted arrays it returns wrong indices silently. How this corrupted patient records.",
  "keywords": "binarySearch() on Unsorted Arrays — Silent Index Corruption, Java, Beginner, Arrays, binarySearch returns -(insertion point)-1, not -1. On unsorted arrays it returns wrong indices silently. How this corrupted patient records.",
  "proficiencyLevel": "Beginner",
  "author": {
    "@type": "Person",
    "@id": "https://thecodeforge.io/about/#naren",
    "name": "Naren",
    "url": "https://thecodeforge.io/about/"
  },
  "publisher": {
    "@type": "Organization",
    "@id": "https://thecodeforge.io/#org",
    "name": "TheCodeForge",
    "url": "https://thecodeforge.io",
    "logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
  },
  "datePublished": "2026-03-05T00:00:00+00:00",
  "dateModified": "2026-08-30T00:00:00+00:00",
  "url": "https://thecodeforge.io/java/array-searching-java/",
  "inLanguage": "en",
  "educationalLevel": "Beginner",
  "image": "https://thecodeforge.io/assets/og-image.png",
  "isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [
    {"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
    {"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
    {"@type": "ListItem", "position": 3, "name": "binarySearch() on Unsorted Arrays — Silent Index Corruption", "item": "https://thecodeforge.io/java/array-searching-java/"}
  ]
}
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [
    {
      "@type": "Question",
      "name": "What is the difference between Arrays.binarySearch() and Collections.binarySearch()?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Arrays.binarySearch() works on primitive or object arrays. Collections.binarySearch() works on List objects. Both require the data to be sorted and return the same negative encoding for not-found. Performance of Collections.binarySearch() depends on the List implementation — O(log n) for ArrayList, O(n log n) for LinkedList."
      }
    },
    {
      "@type": "Question",
      "name": "Can binary search be used on an array with duplicate values?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Yes, but the standard implementation does not guarantee which index of the duplicate it returns — it may return any matching index. If you need the first or last occurrence, you must modify the algorithm (e.g., after finding a match, search leftwards)."
      }
    },
    {
      "@type": "Question",
      "name": "How do I handle integer overflow in midpoint calculation?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Use `mid = left + (right - left) / 2` instead of `(left + right) / 2`. The latter can overflow for very large left and right (e.g., left = 2e9, right = 2e9). The former is safe."
      }
    },
    {
      "@type": "Question",
      "name": "What is the space complexity of recursive binary search?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Recursive binary search uses O(log n) stack space because each recursive call adds a frame to the call stack. Iterative binary search uses O(1) space."
      }
    }
  ]
}

og:typewebsite
og:imagehttps://thecodeforge.io/assets/og-image.png
og:image:width1200
og:image:height630

Links:

TheCodeForge https://thecodeforge.io/
Javahttps://thecodeforge.io/java/
Buy me a coffee https://ko-fi.com/thecodeforge
1Arrays in Javahttps://thecodeforge.io/java/arrays-java/
2Multi-dimensional Arrays in Javahttps://thecodeforge.io/java/multidimensional-arrays-java/
3Array Sorting in Javahttps://thecodeforge.io/java/array-sorting-java/
4Array Searching in Javahttps://thecodeforge.io/java/array-searching-java/
5Arrays Class in Javahttps://thecodeforge.io/java/arrays-class-java/
6Jagged Arrays in Javahttps://thecodeforge.io/java/jagged-arrays-java/
7Copying Arrays in Javahttps://thecodeforge.io/java/copying-arrays-java/
8Sparse Arrays in Javahttps://thecodeforge.io/java/sparse-arrays-java/
← Back to Java track https://thecodeforge.io/java/
Homehttps://thecodeforge.io/
Javahttps://thecodeforge.io/java/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
 ● Production Incidenthttps://thecodeforge.io/java/array-searching-java/#v2-production-incident
 🔎 Debug Guidehttps://thecodeforge.io/java/array-searching-java/#v2-debug-guide
 ⚙ Triage Commandshttps://thecodeforge.io/java/array-searching-java/#v2-triage-commands
Arrayshttps://thecodeforge.io/java/arrays-java/
arrayhttps://thecodeforge.io/dsa/array-data-structure/
linear searchhttps://thecodeforge.io/dsa/linear-search-algorithm/
binary searchhttps://thecodeforge.io/dsa/binary-search-algorithm/
time complexityhttps://thecodeforge.io/dsa/time-complexity-analysis/
Space Complexityhttps://thecodeforge.io/dsa/space-complexity-analysis/
stackhttps://thecodeforge.io/dsa/stack-data-structure/
while loophttps://thecodeforge.io/python/while-loop-python/
recursionhttps://thecodeforge.io/dsa/introduction-recursion/
Jump Searchhttps://thecodeforge.io/dsa/jump-search-algorithm/
Interpolation Searchhttps://thecodeforge.io/dsa/interpolation-search/
→Binary Search Overflow — The Bug Hidden in Java for 9 YearsBinary search breaks silently on large arrays when mid calculation overflows. Learn the real production incident, safe fhttps://thecodeforge.io/dsa/binary-search-algorithm/
→Binary Search Interview Problems — Off-by-One TrapsUsing < instead of <= when left == mid breaks rotated array search silently. Master the 4 core patterns that prevent infhttps://thecodeforge.io/interview/binary-search-interview-problems/
→Arrays.asList() — Why add() Silently Fails in ProductionArrays.asList() returns a fixed-size inner ArrayList where add() throws UnsupportedOperationException. A swallowed excephttps://thecodeforge.io/java/arrays-class-java/
→Java binarySearch — Silent Failures on Unsorted ListsbinarySearch returns arbitrary indices on unsorted lists without throwing exceptions. Production searches fail silentlyhttps://thecodeforge.io/java/collections-utility-class-java/
→Linear Search — String == Bug Crashes Customer LookupUsing == on strings in linear search silently returns -1, causing database lookups to fail. Avoid this production bug wihttps://thecodeforge.io/dsa/linear-search-algorithm/
Three Sum Triplet ProblemIntermediatehttps://thecodeforge.io/dsa/three-sum-triplet/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←PreviousArray Sorting in Javahttps://thecodeforge.io/java/array-sorting-java/
NextArrays Class in Java→https://thecodeforge.io/java/arrays-class-java/
LinkedIn https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
Facebook https://www.facebook.com/profile.php?id=61583020033815
YouTube https://www.youtube.com/channel/UCMar7CF2EmCF1I2tw1tNVuQ
Javahttps://thecodeforge.io/java/
Pythonhttps://thecodeforge.io/python/
C / C++https://thecodeforge.io/c-cpp/
JavaScripthttps://thecodeforge.io/javascript/
DSAhttps://thecodeforge.io/dsa/
Databasehttps://thecodeforge.io/database/
System Designhttps://thecodeforge.io/system-design/
DevOpshttps://thecodeforge.io/devops/
C# / .NEThttps://thecodeforge.io/csharp/
PHPhttps://thecodeforge.io/php/
ML / AIhttps://thecodeforge.io/ml-ai/
Interviewhttps://thecodeforge.io/interview/
CS Fundamentalshttps://thecodeforge.io/cs-fundamentals/
Rusthttps://thecodeforge.io/rust/
Toolshttps://thecodeforge.io/tools/
Cheatsheetshttps://thecodeforge.io/cheatsheets/
Mobilehttps://thecodeforge.io/mobile/
Data Analyticshttps://thecodeforge.io/data-analytics/
Securityhttps://thecodeforge.io/security/
Frontendhttps://thecodeforge.io/frontend/
Data Engineeringhttps://thecodeforge.io/data-engineering/
Testinghttps://thecodeforge.io/testing/
Cloudhttps://thecodeforge.io/cloud/
Observabilityhttps://thecodeforge.io/observability/
Web Platformhttps://thecodeforge.io/web-platform/
🧯 Errors Explainedhttps://thecodeforge.io/errors/
⚒ Blueprintshttps://thecodeforge.io/blueprints/
⌨ Practicehttps://thecodeforge.io/practice/
Interview Prephttps://thecodeforge.io/interview/
DSA Problemshttps://thecodeforge.io/dsa/
Developer Toolshttps://thecodeforge.io/tools/
Cheat Sheetshttps://thecodeforge.io/cheatsheets/
Abouthttps://thecodeforge.io/about/
Sitemaphttps://thecodeforge.io/sitemap.xml
Privacyhttps://thecodeforge.io/privacy.html

Viewport: width=device-width, initial-scale=1.0

Robots: max-image-preview:large


URLs of crawlers that visited me.