Ayush7-BIT / turbo-robot

"A collection of essential coding algorithms and popular code solutions. Optimized for interviews and competitive programming, covering a wide range of topics from sorting to dynamic programming.”
17 stars 224 forks source link

Python code for binary search algorithm #115

Open hetvi20 opened 1 week ago

hetvi20 commented 1 week ago

@Ayush7-BIT Could you assign me I have create title of problem and provide solution. 1) Time Complexity: Best case (when the target is at the middle): O(1) Worst case (when the search space is halved until only one element remains): O(log n), where n is the number of elements in the array.

2) Dynamic Input:

The list is dynamically taken as input using input() and converted to integers using map(int, input().split()). The target is also taken dynamically using input().

3) User-Friendly Output:

The result is printed using f-strings to insert variables inside the output message.

Code:

def binarysearch(arry, target) low= 0 high = len(len)-1 while low<= high: mid= (low+high)//2 if arry[mid]== target: return mid elif arry[mid] < target: low= mid+1 else: high= mid-1

return -1

arry= list(map(int, input("enter number separate by space").split() #take a space-separated list of integers and convert them into an integer list using map, input, spilt target= int(input("Enter the target number"))

result = binarysearch(arry, target)

if result!= -1: print(f"Target found at index {result}") #f is use to print value in the string value else: print("Target not found")

Mustafa1765 commented 1 week ago

can u assign me this issue please!

chndrapratapsingh commented 1 week ago

Hey @Ayush7-BIT , i am solve this issue , so can you marge my pull request

Avi10jana commented 1 week ago

hey @Ayush7-BIT , I am interested in this issue, can you assign this issue me

AyushJainSparsh commented 5 days ago

@Ayush7-BIT i want to solve this issue as a beginner level can you assign this to me