W3Schools Java Binary Search: Master the Algorithm with Code Examples

Robert Jul 13, 2026

When it comes to searching for data in arrays, binary search is a powerful and efficient algorithm offered by Java. W3Schools, a renowned web developer learning platform, provides an excellent guide on Java's binary search, which we will delve into in this article. By the end, you'll have a comprehensive understanding of how binary search works in Java and how to implement it.

30 Days of Java – Day 25 | Linear vs Binary Search Explained for Beginners
30 Days of Java – Day 25 | Linear vs Binary Search Explained for Beginners

Before we jump into the details, let's first understand what binary search is. Unlike linear search that checks every element one by one, binary search splits the search interval in half. This halves the search space at each step, resulting in significant time savings, particularly for large data sets.

Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews

Java Binary Search Algorithm

The Java binary search algorithm follows these steps: It first checks if the middle element of the array is the target element. If it is, the search is complete. If not, it checks if the target is in the first half or the second half of the current range. It then selects the appropriate half and repeats the process.

Iterative and Recursive Binary Search Algorithm Implementation in Java
Iterative and Recursive Binary Search Algorithm Implementation in Java

The binary search algorithm is highly efficient, with a time complexity of O(log n). This ensures that large arrays are searched quickly, making it a go-to algorithm for sorted arrays.

Implementing Binary Search in Java

How to code Binary Search Algorithm using Recursion in Java? Example
How to code Binary Search Algorithm using Recursion in Java? Example

To implement binary search in Java, you can use the following Java method. This method takes an array of sorted integers and a target value as inputs and returns the index of the target value if it's found, or -1 if it's not.

Here's an example of how you might implement binary search in Java: ```java public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } ```

Applications of Binary Search in Java

binary search algorithm
binary search algorithm

Binary search has many applications in Java. It is used in data structures like java.util.Arrays.binarySearch(), where it's used to search for a specific element in a sorted array. It is also utilized in search engines to retrieve data from large databases, and in autocomplete suggestions in text fields.

Let's look at how you can use the binarySearch method in a simple application. ```java public static void main(String[] args) { int[] arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}; int target = 23; int result = binarySearch(arr, target); if (result == -1) System.out.println("Element not present in array"); else System.out.println("Element found at index " + result); } ```

With this code, we're searching for the value 23 in the array. If it's found, we print out its index; otherwise, we print out a message saying that the element is not in the array.

Binary Search in Java for Sorted Array
Binary Search in Java for Sorted Array

Handling Duplicate Values in Binary Search

When your array contains duplicate values, the above algorithm might not always return the first occurrence. You need to change the comparison slightly to ensure that. Here's an updated binarySearch method that can handle duplicate values:

How to Implement Binary Search Tree in Java? Example
How to Implement Binary Search Tree in Java? Example
Binary Search Explained Visually | DSA Guide for Beginners
Binary Search Explained Visually | DSA Guide for Beginners
Java - Add two binary numbers
Java - Add two binary numbers
a table with numbers and an arrow pointing to it
a table with numbers and an arrow pointing to it
Binary Search Tree (BST) Cheat Sheet 🌳 | Easy DSA Notes for Beginners
Binary Search Tree (BST) Cheat Sheet 🌳 | Easy DSA Notes for Beginners
AP COMPUTER SCIENCE notes on binary search in java learn to code for beginners
AP COMPUTER SCIENCE notes on binary search in java learn to code for beginners
Binary Search
Binary Search
an image of a black screen with text on it and the words day 52 / 2 / 20 search in python
an image of a black screen with text on it and the words day 52 / 2 / 20 search in python
an image of a computer screen with the text library search in white and black on it
an image of a computer screen with the text library search in white and black on it
an image of a computer screen with text
an image of a computer screen with text
Binary search tree implementation
Binary search tree implementation
80+ Pattern Programs In Java
80+ Pattern Programs In Java
Binary to Octal in Java
Binary to Octal in Java
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
java array operations
java array operations
The Origins of Language | Japanese Font Download 897.27.3.10128
The Origins of Language | Japanese Font Download 897.27.3.10128
java at a glance 😎
java at a glance 😎
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Bubble short snap
Bubble short snap
an image of a computer screen with many lines on the screen and numbers in different colors
an image of a computer screen with many lines on the screen and numbers in different colors

Note: This is a vast topic and covers more than we can delve into in this article.

With this understanding of binary search in Java, you're now equipped to use this efficient algorithm in your own coding projects. Happy coding!