defBinarySearch(array,num,first,last):if(first>last):print("\nElement not found!")print("———————————————————————————————————————————")else:mid=(first+last)//2;if(array[mid]==num):print("\nElement found at index",mid+1)print("———————————————————————————————————————————")elif(array[mid]>num):BinarySearch(array,num,first,mid-1);else:BinarySearch(array,num,mid+1,last);print("———————————————————————————————————————————")print("Implementation of a Binary Search")print("———————————————————————————————————————————")try:array=[]n=int(input("How many numbers you want to enter "))if(n>0):foriinrange(0,n):print("Enter Number",i+1,end=" | ")numbers=int(input())array.append(numbers)array.sort()print("\nSorted numbers",array)num=int(input("\nEnter the number to be searched "))beg=0end=n-1BinarySearch(array,num,beg,end)else:print("\nPlease enter a positive integer")print("———————————————————————————————————————————")exceptValueError:print("\nInvalid Input!\nProgram Terminated")print("———————————————————————————————————————————")
Output: positive test
kodingwindow@kw:~$ python3 kw.py
———————————————————————————————————————————
Implementation of a Binary Search
———————————————————————————————————————————
How many numbers you want to enter 8
Enter Number 1 | -29
Enter Number 2 | 85
Enter Number 3 | -1024
Enter Number 4 | 2048
Enter Number 5 | 596
Enter Number 6 | 100
Enter Number 7 | 65535
Enter Number 8 | 0
Sorted numbers [-1024, -29, 0, 85, 100, 596, 2048, 65535]
Enter the number to be searched 0
Element found at index 3
———————————————————————————————————————————
kodingwindow@kw:~$
kodingwindow@kw:~$ python3 kw.py
———————————————————————————————————————————
Implementation of a Binary Search
———————————————————————————————————————————
How many numbers you want to enter 5
Enter Number 1 | 2
Enter Number 2 | 2
Enter Number 3 | 2
Enter Number 4 | 2
Enter Number 5 | 2
Sorted numbers [2, 2, 2, 2, 2]
Enter the number to be searched 2
Element found at index 3
———————————————————————————————————————————
kodingwindow@kw:~$
Output: negative test
kodingwindow@kw:~$ python3 kw.py
———————————————————————————————————————————
Implementation of a Binary Search
———————————————————————————————————————————
How many numbers you want to enter 5
Enter Number 1 | 8
Enter Number 2 | 5
Enter Number 3 | 2
Enter Number 4 | #
Invalid Input!
Program Terminated
———————————————————————————————————————————
kodingwindow@kw:~$
kodingwindow@kw:~$ python3 kw.py
———————————————————————————————————————————
Implementation of a Binary Search
———————————————————————————————————————————
How many numbers you want to enter 0
Please enter a positive integer
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?