简体   繁体   中英

Python repeated output from program

I created a binary search but I am stuck at a problem. Whenever I run main( ) I just get None for when key equals 8, 5, 2, and 1. I also am trying to have bsearch return the index of the key_point value in integer_list but instead it just returns the key_point. If someone could let me know what is going wrong I would appreciate it.

def bsearch(integer_list, key_point, start, end):
    midpoint = (start + end) // 2
    if start >= end:
        return None
    elif integer_list[midpoint] == key_point:
        return key_point
    elif integer_list[midpoint] > key_point:
        return bsearch(integer_list, key_point, start, (midpoint - 1))
    elif integer_list[midpoint] < key_point:
        return bsearch(integer_list, key_point, (midpoint + 1), end)


def main( ):
    integer_list = [x + 1 for x in range(0, 10)]
    key = 11
    p = len(integer_list) - 1
    start = integer_list[0]
    end = integer_list[p]
    while key >= 1:
        bsearch(integer_list, key, start, end)
        print(bsearch(integer_list, key, start, end))
        key = key - 1

You have following errors:

1) The call to binary_search from main contains start = 1 and end = 10 which should have been start = 0 and end = 9 (value of the indices).We find all the mid points according to indices.

2) Just check start > end because start == end is absolutely correct and the cases where you got a None were all due to this case. All the cases where you returned due to this condition were actually the case where integer_list[midpoint] == key_point holded true.

Here's the corrected code:

def bsearch(integer_list, key_point, start, end):
    midpoint = (start + end) // 2
    if start > end:
        return None
    elif integer_list[midpoint] == key_point:
        return key_point
    elif integer_list[midpoint] > key_point:
        return bsearch(integer_list, key_point, start, (midpoint - 1))
    elif integer_list[midpoint] < key_point:
        return bsearch(integer_list, key_point, (midpoint + 1), end)


def main():
    integer_list = [x + 1 for x in range(0, 10)]
    key = 11
    p = len(integer_list) - 1
    start = integer_list[0]
    end = integer_list[p]
    while key >= 1:
        print(bsearch(integer_list, key, 0, p))
        key = key - 1
main()       

Hope that helps :)

You're mixing up indexes and values several times in your algorithm. Your base/found case should be returning midpoint since that's the place you found it, but you're returning key_point, the value you're actually searching for instead.

And again, when you're setting up your loop, you're setting your start and end values to be the contents of the List at the beginning and end, instead of the beginning and ending index. Some of these errors will become more noticeable if for your test_list you use a set of numbers that isn't just sequential numbers 1-11. Right now it just happens to line up.

First of all, you are passing values to the function instead of indexes:

start = 0
end = len(integer_list) - 1

Second you need to return None when both start and end are reversed. not when they are equal

if start > end:
        return None

Third, why would you return the value you are searching for? it does not make any sense. You should return the index of the element instead.

elif integer_list[midpoint] == key_point:
        return mid_point

Finally, if you want to follow what almost every search method returns, you should return -1 when you can't find the value you are looking for, instead of None. This will save you some headaches.

if start > end:
        return -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM