77
Binary Search in Java
Binary search is used to search a key element from multiple elements. Binary search is faster than linear search.
In case of binary search, array elements must be in ascending order. If you have unsorted array, you can sort the array using Arrays.sort(arr) method.
Binary Search Example in Java
Let’s see an example of binary search in java.
Output:
Element is found at index: 2
Binary Search Example in Java using Recursion
Let’s see an example of binary search in java where we are going to search an element from an array using recursion.
Output:
Element is found at index: 2
Binary Search Example in Java using Arrays.binarySearch()
Output:
Element is found at index: 2
Next TopicJava Programs