Binary Search In Python | Fastest Searching Algorithm In Python | Python Program for binary search

Fastest Searching Algorithm In Python

binary search


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.


FAQ

What is the use of the Binary Search Algorithm?

Binary Search Algorithm is used to search elements from the list in the fastest way.

How Binary Search Algorithm works?

Binary Search Algorithm, first cut the list from the mid then search the elements from the left, if they found the elements from the left side then again cut the left side from the mid and again do this step if element not found from the left side then go to the right side and cut the list from the mid and again repeat this step and in the last 1 elements remain and if this element is equal to the elements that we want to search then it gives index value of the elements, otherwise it gives elements not found.


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


Next Post Previous Post
1 Comments
  • Embassy Education
    Embassy Education December 18, 2020 at 2:58 AM

    It's a nice article, Which you have shared here about thepython coding. Your article is very informative and I really liked the way you expressed your views in this post. Thank you. python coding

Add Comment
comment url