# Binary Search In Python

There are lot’s of algorithms for searching data in a data set. But the most convenient algorithm is the **Binary Search.**

**Binary search** is the most popular Search algorithm. It is efficient and also one of the most commonly used techniques that is used to solve problems.

If all the names in the world are written down together in order and you want to search for the position of a specific name, binary search will accomplish this in a maximum of **35** iterations.

Binary search works only on a sorted set of elements. To use binary search on a collection, the collection must first be sorted.

We will prefer **Binary search upon Linear search** because binary search is less time and memory consuming. In **Linear search** we need to compare the data we want to search with each and every data in the data set. But, it’s not the same case in **Binary search.**

# How Binary Search Works ?

Binary search works by comparing the target value to the middle element of the array. If the target value is greater than the middle element, the left half of the list is eliminated from the search space, and the search continues in the right half. If the target value is less than the middle value, the right half is eliminated from the search space, and the search continues in the left half. This process is repeated until the middle element is equal to the target value, or if the algorithm returns that the element is not in the list at all.

# Implementing Binary Search

- Start with the value at mid index value index:

- If the
**search**value is equal to the value at**mid index value**index of the array or collection, then return the index of the middle element. - If not, then compare the middle element with the target value.
- If the target value is greater than the number in the middle index, then pick the elements to the right of the middle index, and start with Step 1.
- If the target value is less than the number in the middle index, then pick the elements to the left of the middle index, and start with Step 1.

2. When a match is found, return the index of the element matched.

3. If no match is found, then return “No Found ! ”

# Implementing Binary Search In Python

`list = [2, 5, 8, 12, 16, 23, 38, 91, 56, 72]`

def search(number):

lower_index_value = 0

upper_index_value = len(list) - 1

list.sort()

print(f"We had sorted the list for you if you didn't : {list}")

while lower_index_value <= upper_index_value:

mid_val = (lower_index_value + upper_index_value) // 2

if list[mid_val] == number:

return f"We found the number at index {mid_val}"

else:

if number < list[mid_val]:

upper_index_value = mid_val - 1

else:

lower_index_value = mid_val + 1

return "Not Found !"

print(search(23))

print(search(78))

**Output :**

`We had sorted the list for you if you didn't : [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] `

We found the number at index 5

We had sorted the list for you if you didn't : [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]

Number not found !