Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
43.7k views
in Computer by (53.4k points)

Write a Recursive function in python BinarySearch(Arr,l,R,X) to search the given element X to be searched from the List Arr having R elements where l represents lower bound and R represents upper bound.

1 Answer

+1 vote
by (53.3k points)
selected by
 
Best answer

def BinarySearch (Arr,l,R,X):

if R >= l:

mid = l + (R-l)//2

if Arr[mid] == X:

return mid

elif Arr[mid] > X: 

return BinarySearch(Arr,l,mid-1,X)

else:

return BinarySearch(Arr,mid+1,r,X)

else: return -1

Arr = [ 2, 3, 4, 10, 40 ]

X =int(input(' enter element to be searched'))

result = BinarySearch(Arr,0,len(Arr)-1,X)

if result != -1:

print ("Element is present at index ", result)

else:

print ("Element is not present in array")

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...