BINARY SEARCH In Python |Fastest Searching Algorithm
Here the Python coding only for so we will introduce about coding in computer that is python language .
Now we will make a program on BINARY SEARCH ALGORITHM
Binary search is fastest and simple searching program to search the element from the list.This program is start from from starting index as well as from ending index value from the list .
Q.Write a program to search a element from the list using Binary Search Algorithm ?
Steps:
step1:Let understand the progarm and here we are searching the elemnt using bimary search i.e from sides at the single moment,Then input the list using input() function . then input searching element
step2:assume that element is not in the list and i statring index i.e 0 and j last index len(List)-1.
step3:Then use while loop and give command that i value is less than or equal to value of j till this loop will run.
step4:mid means middle index value in the list ,but here mid will be middle index ,then test condition if the value of middle index match to search element then flag=1.
step5:In the elif part if searching element is greater than middle index value ,Increase the value of i by 1 .i e to check for next index.
step6:or in else part i.e value of searching element is less than middle index value using Relational operator ,Then decrease the value of j by 1.
step7:after testing the condition out of the loop then compare the value of flag if flag is 1 then print found using print() , otherwise then in else print not found.
Testcases1:
Input:
[1,2,3,4,5]
4
Output:
Found
Testcases2:
Input;
[1,2,667,89,89,60]
100
Output:
Not found
"""
WAP to search element using binary seacrh
# i= 0 1 2 3 4 = j
# List = [2, 4, 6, 10, 19]
# ele = 20
"""
#Let understand the progarm and here we are searching the elemnt using bimary search i.e from sides at the single moment
#Then input the list using input() function
List = eval(input("Enter a list of number(In ASC order only):"))
#then input searching element
ele=int(input("Enter an element to be serach in the list:"))
flag = 0 # assume that element is not in the list
i=0 # statring index i.e 0
j = len(List)-1 # last index len(List)-1
#Then use while loop and give command that i value is less than or equal to value of j till this loop will run
while(i<=j):
# mid means middle index value in the list ,but here mid will be middle index
mid=(i +j)//2
# test condition if the value of middle index match to search element then flag=1
if List[mid] == ele:
flag =1
break
#In the elif part if searching element is greater than middle index value
#Increase the value of i by 1 .i e to check for next index
elif ele>List[mid]:
i = mid +1
#or in else part i.e value of searching element is less than middle index value using Relational operator
#Then decrease the value of j by 1
else:
j = mid-1
#after testing the condition out of the loop then compare the value of flag if flag is 1 then print found using print()
if flag == 1:
print("Element found in the list.")
#or in else print not found
else:
print("Element not found......")
No comments: