We are going to learn about one of the fastest searching methods, which is the Binary Search. This searching method is also known as half-interval search, logarithm search, or binary chop. Always remember, this searching method works only on a sorted  array_ or _ list_. A _binary search is used to search the key item from multiple items. Binary search is faster than a linear search.

Java Binary Search

In the case of binary search, array elements must be in ascending order. If you have an unsorted array, you can sort the array using Arrays.sort(arr) method.

Binary Search works like the following.

Search the sorted array by repeatedly dividing the search interval in half.

Begin with the interval covering the whole array.

If a value of the search key is less than an element in the middle of the interval, narrow the interval to the lower half.

Otherwise, narrow it to the upper half. Repeatedly check until a value is found or an interval is empty.

#java #arrays.sort(arr) #java binary search

Java Binary Search Program | Binary Search In Java Example
2.20 GEEK