python --二分法學習

正霜霜儿發表於2024-04-19
def found_number(need_vaule,l):
    print(l)
    mid_index=len(l) // 2
    mid_value=l[mid_index]
    print("mid_value is %s"%(mid_value))
    if mid_value > need_vaule:
        l=l[:mid_index]
        print('need to find1')
        found_number(need_vaule,l)
    elif mid_value < need_vaule:
        l=l[mid_index+1:]
        print("need to find 2")
        found_number(need_vaule,l)
    else:
        print("find it")
l=[1,2,4,6,8,10,11,44,66,99]
found_number(44,l)
python --二分法學習
/usr/local/bin/python3.8 /Users/futantan/PycharmProjects/S14/函式的學習/二分法.py
[1, 2, 4, 6, 8, 10, 11, 44, 66, 99]
mid_value is 10
need to find 2
[11, 44, 66, 99]
mid_value is 66
need to find1
[11, 44]
mid_value is 44
find it

Process finished with exit code 0
View Code

相關文章