Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15940

Getting IndexError when using recursion to write a BinarySearch method

$
0
0
def binarySearch2(arr, val):    left = 0    right = len(arr) - 1    mid = (left + right) // 2    arr.sort()    if val == arr[mid]:        return True    if val > arr[mid]:        return binarySearch2(arr[mid + 1:], val)    if val < arr[mid]:        return binarySearch2(arr[:mid - 1], val)    for i in range(10):        print(binarySearch2([1, 2, 3, 4, 5, 6, 7, 8, 9], i))

It keeps telling me index out of range and i'm not sure where went wrong.


Viewing all articles
Browse latest Browse all 15940


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>