تابعی به صورت بازگشتی بنویسید که در آرایه ای جستجوی دودویی انجام دهد
ARRAYSIZE = 10
sortedArray = [8,67,230,234,400,410,799,810,903,1235] # Test array
# Python Program for recursive binary search
#=========<< recursiveBinarySearch >>===========
def recursiveBinarySearch(start, end , key ) :
if start < end : # Check base case
mid = start +(end - start ) // 2 # If element is present at the middle itself
if key < sortedArray[mid] :
return recursiveBinarySearch(start, mid , key )
elif key > sortedArray[mid] :
return recursiveBinarySearch(mid+1 , end , key ) # Else the element can only be present
# in right subarray
else :
return mid # If element is smaller than mid, then it
# can only be present in left subarray
return -1 # Element is not present in the array
#==============<< MAIN >>=====================
number = int(input("Please enter a number :"))
index = recursiveBinarySearch(0 , ARRAYSIZE , number ) # Function call
if index >= 0 :
print("Found",number,"at",index,"index")
else :
print(number," Not Found !!! ")