0
def binary_search(array,key,left,right):
    if left>right:
        return -1
    mid=(right+left)//2

    if key==array[mid]:
        return mid
    i=0
    if key<array[mid]:
        i+=1
        print("subarray at step {} : {}".format(i,array[left:mid]))
        return binary_search(array,key,left,mid-1)

    elif key>array[mid]:
        i+=1
        print("subarray at step {} : {}".format(i,array[mid:right]))
        return binary_search(array,key,mid+1,right)



array=[1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,222,333]
res= binary_search(array,88,0,len(array))

print(res if res!=-1 else "Not found")

In this binary search code, I couldn't figure out why the counter wan not working. Each time the i is printed a 1. The counter doesn't increase. What am I doing wrong? Thank you.

4
  • 1
    Because you set i = 0 each time before incrementing it... Commented Oct 4, 2020 at 18:00
  • If you are using an IDE now is a good time to learn its debugging features - like setting breakpoints and examining values. Or you could spend a little time and get familiar with the built-in Python debugger. Also, printing stuff at strategic points in your program can help you trace what is or isn't happening. Commented Oct 4, 2020 at 18:00
  • You're calling the search function recursively. For each call, only a section of the full array is passed. i always starts as 0 for the section. Commented Oct 4, 2020 at 18:01
  • @wwii I tried using the debugger in pycharm a few times. But I couldn't figure out how. Do you have any blogs or youtube vidoes I could refer to learn debugger. Commented Oct 4, 2020 at 18:13

2 Answers 2

1

i is being printed as 1 because you are i is being set to 0 inside binary_search function on each call. try moving out initiaization of i outside the function.


def binary_search(array,key,left,right):
    global i
    if left>right:
        return -1
    mid=(right+left)//2

    if key==array[mid]:
        return mid
    # i=0
    if key<array[mid]:
        i+=1
        print("subarray at step {} : {}".format(i,array[left:mid]))
        return binary_search(array,key,left,mid-1)

    elif key>array[mid]:
        i+=1
        print("subarray at step {} : {}".format(i,array[mid:right]))
        return binary_search(array,key,mid+1,right)


i =0
array=[1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,222,333]
res= binary_search(array,88,0,len(array))

You need to change the scope of variable i to make it global.

Sign up to request clarification or add additional context in comments.

Comments

0

make i as global variable and to assing valuable call global keywoard

i = 0
def binary_search(array,key,left,right):
    if left>right:
        return -1
    mid=(right+left)//2
    # i=0

    if key==array[mid]:
        return f'it found position {mid}'
    
    if key<array[mid]:
        global i 
        i = i+1
        print("subarray at step {} : {}".format(i,array[left:mid]))
        return binary_search(array,key,left,mid-1)

    elif key>array[mid]:
        i+=1
        print("subarray at step {} : {}".format(i,array[mid:right]))
        return binary_search(array,key,mid+1,right)



array=[1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,222,333]
res= binary_search(array,88,0,len(array))
print(len(array))

print(res if res!=-1 else "Not found")

2 Comments

Shouldn't you have to give global while declaring the variable? This code works. Like global i i=0
no you dont check the third if I call there the global keywoard global i otherwise you will get an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.