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/
Domain: thecodeforge.io
{
"@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:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large