Binary Search Algorithm in Java

Below is the Algorithm designed for Binary Search:

  1. Start
  2. Take input array and Target
  3. Initialise start = 0 and end = (array size -1)
  4. Intialise mid variable
  5. mid = (start+end)/2
  6. if array[ mid ] == target then return mid
  7. if array[ mid ] < target then start = mid+1
  8. if array[ mid ] > target then end = mid-1
  9. if start<=end then goto step 5
  10. return -1 as Not element found
  11. Exit

Now you must be thinking what if the input is not sorted then the results are undefined.

Note: If there are duplicates, there is no guarantee which one will be found.

Binary Search in Java

Binary search is one of the searching techniques applied when the input is sorted here we are focusing on finding the middle element that acts as a reference frame whether to go left or right to it as the elements are already sorted. This searching helps in optimizing the search technique with every iteration is referred to as binary search and readers do stress over it as it is indirectly applied in solving questions.

Similar Reads

Binary Search Algorithm in Java

Below is the Algorithm designed for Binary Search:...

Methods for Java Binary Search

There are three methods in Java to implement Binary Search in Java are mentioned below:...

Binary Search in Java Collections

...