Binary Search JavaLast Updated : 31 Jan 2026 Binary search is an efficient algorithm used to search target element in a sorted array or a list. It follows the divide and conquer approach. It is faster than linear search. Note that an array or list must be sorted before performing the binary search. If we have an unsorted array or list, we can sort the array using the Arrays.sort(arr) method. Working of Binary Search
Binary Search AlgorithmBinary Search ExampleConsider the following array. We have taken a sorted array in which we have to search for the value 28 (target element). ![]() The target element is greater than the current mid element (i.e. 28). The search moves to the right subarray. In the right subarray, the middle element is 32, and the target element (28) is less than the middle element. So, the search moves to the right of the array. We found the target element. Stop the search. ![]() Approaches for Binary SearchThere are the following three ways to perform a binary search:
Binary Search ExampleLet's implement binary search logic in a Java program. ExampleCompile and RunOutput: Element found at index: 3 Binary Search Using the Iterative ApproachThe Iterative Method for Binary Search in Java is a straightforward and efficient technique used to find the position of a target element in a sorted array. This approach uses a while loop to reduce the search range by half after each iteration, adjusting the start and end indices based on the comparison with the target value. ExampleCompile and RunOutput: Element found at index: 4 Binary Search Using RecursionThe recursive approach splits the array in half during each call, narrowing down the search area by focusing on either the left or right segment based on the comparison with the midpoint value. The recursion continues until the target is found or the search space is empty, at which point the algorithm terminates. Let's implement the recursive approach in a Java program. ExampleCompile and RunOutput: Element is found at index: 2 Binary Search Using Arrays.binarySearch() MethodThe Java Arrays class provides different variants of the binarySearch() method for byte, char, int, float, double, long, object, and short data types. It is a static method. The method searches specified value using the binary search algorithm. The array must be sorted (as by the sort(int[]) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found. Syntax: The method takes two arguments, an array to be search for, and the element (key) to be searched for. When the target element is found, it returns the index value of the element; otherwise, it returns a negative value indicating the insertion point to keep the array sorted. ExampleCompile and RunOutput: Element found at index: 4 Binary Search Using Collections.binarySearch() MethodThe Java Collections class also provides the built-in binarySearch() method. The method searches for the specified object using the binary search algorithm. The list must be sorted in ascending order. If the list is not sorted, the results are undefined. If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found. It simplifies searching operations in Java Collections, offering a standardized and optimized approach for locating elements. Syntax: The method accepts two parameters: the list to be search and the key element to be searched for. It returns the index value of the key element. ExampleCompile and RunOutput: Element found at index: 5 Complexity AnalysisTime Complexity In binary search, at each step, the algorithm divides the array by 2. After k steps, the size becomes n / 2k. When we solve n / 2k = 1, we get k = log₂(n).
Space Complexity
Next TopicJava Programs |
We request you to subscribe our newsletter for upcoming updates.