Why does my Python binary search algorithm not work for certain items?
Question:
My binary search algorithm manages to find some items but does not find others:
myList = [1,3,4,7,12,13,14,16,19,20,28,29,40,45,48,50,67,89,91,94]
item = 67
found = False
lowerBound = 0
upperBound = len(myList) - 1
index = 0
while not found and lowerBound != upperBound:
index = (upperBound+lowerBound) // 2
if item == myList[index]:
found=True
if item > myList[index]:
lowerBound = index+1
if item < myList[index]:
upperBound = index-1
if found:
print('Item found')
else:
print('Item not found')
For example, it manages to find 91, 89, 50, 48, 40, 29, etc., but doesn’t find 94, 67, 45, or 28.
There seems to be a pattern in the numbers it finds and those it doesn’t. Did I make a mistake somewhere in the code?
Answers:
Use while not found and lowerBound <= upperBound
Listing iteration of 67
(lowerBound,upperBound)
(0,19)
(10,19)
(15,19)
(15,16)
(16,16)
While you algorithm make exit at same lowerBound and upperBound
The problem is caused by the statement in the while loop. You wrote while lowerBound != upperBound, but that is wrong because if the value at that index is the right value, then you just exit the loop without checking. That can be fixed by using a different condition which is lowerBound <= upperBound. That is a safer and more correct condition to use in this case. The code with the correction made is bellow:
myList = [1,3,4,7,12,13,14,16,19,20,28,29,40,45,48,50,67,89,91,94]
item = 94
found = False
lowerBound = 0
upperBound = len(myList) - 1
index = 0
while not found and lowerBound <= upperBound:
index = (upperBound+lowerBound) // 2
if item == myList[index]:
found=True
if item > myList[index]:
lowerBound = index+1
if item < myList[index]:
upperBound = index-1
if found:
print('Item found')
else:
print('Item not found')
My binary search algorithm manages to find some items but does not find others:
myList = [1,3,4,7,12,13,14,16,19,20,28,29,40,45,48,50,67,89,91,94]
item = 67
found = False
lowerBound = 0
upperBound = len(myList) - 1
index = 0
while not found and lowerBound != upperBound:
index = (upperBound+lowerBound) // 2
if item == myList[index]:
found=True
if item > myList[index]:
lowerBound = index+1
if item < myList[index]:
upperBound = index-1
if found:
print('Item found')
else:
print('Item not found')
For example, it manages to find 91, 89, 50, 48, 40, 29, etc., but doesn’t find 94, 67, 45, or 28.
There seems to be a pattern in the numbers it finds and those it doesn’t. Did I make a mistake somewhere in the code?
Use while not found and lowerBound <= upperBound
Listing iteration of 67
(lowerBound,upperBound)
(0,19)
(10,19)
(15,19)
(15,16)
(16,16)
While you algorithm make exit at same lowerBound and upperBound
The problem is caused by the statement in the while loop. You wrote while lowerBound != upperBound, but that is wrong because if the value at that index is the right value, then you just exit the loop without checking. That can be fixed by using a different condition which is lowerBound <= upperBound. That is a safer and more correct condition to use in this case. The code with the correction made is bellow:
myList = [1,3,4,7,12,13,14,16,19,20,28,29,40,45,48,50,67,89,91,94]
item = 94
found = False
lowerBound = 0
upperBound = len(myList) - 1
index = 0
while not found and lowerBound <= upperBound:
index = (upperBound+lowerBound) // 2
if item == myList[index]:
found=True
if item > myList[index]:
lowerBound = index+1
if item < myList[index]:
upperBound = index-1
if found:
print('Item found')
else:
print('Item not found')