# Fastest Searching Algorithm In Python

The fastest searching algorithm is binary search in python.
In this tutorial, we learn how binary search algorithms work and we make a python program of the binary search.

## Step to create Binary Search Program using recursive:

1. We will make a function of the binary search and we take a parameter of lists, high, low, and x.
2. x is a variable for the elements that we want to search.
3. Then we check if the high is greater and equal to the low.
4. And then we cut the list into two parts from the middle.
5. And then check the mid-value to the elements, if elements found then return it otherwise moves to the next step.
6. Then we check the element found on the right side or left. And update the value of low and high according to it.
7. And then again do this that cut the lists from the mid and check from left and right.
8. Repeat this process until the elements found or all the elements are not checked.
9. And if elements found then print the index value of it, otherwise print elements not found.

#### Program:

def binarySearch(listslowhighx):
if high >= low:
mid = (high+low)//2
if lists[mid] == x:
return mid

elif lists[mid]>x:
return binarySearch(listslowmid-1x)
else
return binarySearch(listsmid+1highx)

else
return -1

lists = list(map(int,input("Enter the list: ").strip().split()))
x = eval(input("Enter the Element which you want to search: "))

result = binarySearch(lists0len(lists)-1x)

if result!= -1:
print("Elements Found at index: ",(result))
else:
print("Element Not Found")

#### Output:

Enter the list1 2 3 4 5 6 7 8 11 222 333
Enter the Element which you want to search3
Elements Found at index:  2

## Step to create Binary Search Program using iterative:

1. We will make a function of the binary search and we take a parameter of lists and x.
2. x is a variable for the elements that we want to search.
3. Then take a low value and mid-value to 0 and a high value to the length of list-1.
4. Now we iterated by using a while loop.
5. And then we cut the list into two parts from the middle.
6. And then check the mid-value to the elements, if elements found then return it otherwise moves to the next step.
7. Then we check the element found on the right side or left. And update the value of low and high according to it.
8. And then again do this that cut the lists from the mid and check from left and right.
9. Repeat this process until the elements found or all the elements are not checked.
10. And if elements found then print the index value of it, otherwise print elements not found.

#### Example:

def binarySearch(listsx):
low = 0
high = len(lists) - 1
mid = 0

while low <= high:

mid = (high + low) // 2

if lists[mid] < x:
low = mid + 1

elif lists[mid] > x:
high = mid - 1

else:
return mid
return -1

lists = list(map(int,input("Enter list: ").strip().split()))
x = int(input("Enter Elements to Search: "))

result = binarySearch(listsx)

if result != -1:
print("Element is found at index"result)
else:
print("Element is not found")

#### Output:

Enter list1 2 3 4 5 6 7 8 111 222 333 444 5221
Enter Elements to Search2
Element is found at index 1