# Binary Search Algorithm with Python

Here in this article, I will take you through how to implement a binary search algorithm with python. Binary search also called half-interval search, which is an algorithm used in computers systems to find the position of a value in a sorted array.

## What is Binary Search Algorithm?

In a binary search algorithm, the list is split in half and then searched in each half. One thing to notice while implementing the binary search algorithm is that the list must be sorted before running the algorithm.

Also, Read – One Hot Encoding in Machine Learning.

The list is then split into two halves by the index, find the element in the middle(m) of the list, then start at m-1 is a list and m+1 at the end is another list, check if the element is at the middle, higher or lower than this and return the appropriate position of the key element to find.

## How Binary Search Works?

So let’s say you have a list of 10,000 items. The element you are looking for is in the 9000th place. If you implement any other search algorithm in this scenario, it will take a long time to give you the result. Because the algorithm has to check every item in the list.

So we first need to specify the lower limit and the upper limit. The lower limit is the first index in the list and the upper limit is the last index in the list. Once you finish assigning the lower and upper limits, the next thing you have to find a mid-index.

Mid index = (Lower limit + Upper Limit)/2

Now let’s specify the item we need to find in our list. Let’s say we need to find number 90 in our list. Now let’s create a binary search algorithm with Python to find the number 90 in a list:

`Output: Found`

This is how you do the binary search. If you have a huge list, this method will save your life. There is a major drawback to this method of research. You must have a sorted list if you want to search your list using binary search.