Skip to content

natnew/Python-Projects-Binary-Search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Python Projects: Binary Search 🐍

This repo contains python code that searches whether a given binary is in a list .
Run the code.

Python

lst = [1, 3, 2, 4, 5, 6, 9, 8, 7, 10]
lst.sort()
first = 0
last = len(lst) - 1
mid = (first + last) // 2
item = int(input("enter the number to be search"))
found = False
while (first <= last and not found):
    mid = (first + last) // 2
    if lst[mid] == item:
        print(f"found at location {mid}")
        found = True
    else:
        if item < lst[mid]:
            last = mid - 1
        else:
            first = mid + 1

if found == False:
    print("Number not found")

Output

enter the number to be search1
found at location 0

About

Python Projects Binary Search

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages