# What is a Binary Search?

A binary search, sometimes called a half-interval search, is a type of algorithm that finds a specific value in a sorted array. The algorithm works by comparing the value that you’re looking for with the middle element of the array.

If the value you’re looking for is less than the middle element, then the algorithm searches the left half of the array. If the value you’re looking for is greater than the middle element, then the algorithm searches the right half of the array.

## How Binary Search Works?

A binary search starts by checking to see if the value you’re looking for is in the array. If it’s not in the array, then the binary search returns -1. If the value is in the array, then binary search finds the position of the value in the array and returns that position.

The algorithm works by comparing the value you’re looking for with the middle element of the array. If the value you’re looking for is less than the middle element, then the algorithm searches the left half of the array. If the value you’re looking for is greater than the middle, then it searches the right half of the array.