Skip to content

Spaddy786/Cognizance2020

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binary Search

What is Binary Search?

Binary search

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.

Difference between Linear and Binary Search

Linear Search Binary Search
Linear search is an algorithm to find an element in a list by sequentially checking the elements of the list until finding the matching element Binary search is an algorithm that finds the position of a target value within a sorted array.
A linear search scans one item at a time without skipping to any item. A binary search cuts down the search to half as soon as the middle of a sorted list is found.
Linear search is easy to use because there is no need for any ordered elements. The binary search is a bit complicated with elements being necessarily arranged in a given order.

Program

  def bsearch(L,n):
       start=0
       end=len(L)-1
       while start<=end:
          mid=(start+end)//2
          if L[mid]==n:
            return True
          elif L[mid]<=n:
            start=mid+1
          else:
          end=mid-1
        else:
           return 
           False 
   L=eval(input("Enter the list of numbers"))               
   n=int(input("Enter the list of numbers"))
   L.sort()
   if bsearch(L,n):
        print("Letter found")
   else:
        print("Letter not found")    

Purpose of binary search

  • Binary search is an efficient algorithm for finding an item from a sorted list of items.
  • It works by repeatedly dividing in half the portion of the list that could contain the item.

Example of Binary Search

Games

Usage

  • Eases the tasks in Python
  • Used In #C programming language
  • better than linear search in every aspect
Logon to the following website to know more-

Cognizance2020


Thank You

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published