我试图在 python 中执行二进制搜索程序。我遵循了算法步骤,但它给了我这个错误。这是我的代码:
def binarySearch(a,k,l,r):
if l > r:
return -1
else:
mid = (l+(r-l))//2
if(a[mid]>k):
return binarySearch(a,k,l,mid-1)
elif(a[mid]<k):
return binarySearch(a,k,mid+1,r)
else:
return mid
t = int(input("Enter no. of test cases: "))
for _ in range(t):
n,k = map(int, input().split())
a = list(map(int, input().rstrip().split()))
print(binarySearch(a,k,0,n))
错误:
return binarySearch(a,k,mid+1,r)
File "e:/Coding/Algorithms/Searching/binarySearch.py", line 10, in binarySearch
return binarySearch(a,k,mid+1,r)
File "e:/Coding/Algorithms/Searching/binarySearch.py", line 10, in binarySearch
return binarySearch(a,k,mid+1,r) [Previous line repeated 994 more times]
File "e:/Coding/Algorithms/Searching/binarySearch.py", line 3, in binarySearch if r < l:
RecursionError: maximum recursion depth exceeded in comparison