binarni-hledani.py 425 Bytes
Newer Older
Mareš Martin's avatar
Mareš Martin committed
1 2 3 4 5 6 7 8 9 10
kde = [11, 22, 33, 44, 55, 66, 77, 88]
co = int(input())

# Hledané číslo se nachazí v intervalu [l, p]
l = 0
p = len(kde) - 1

while l <= p:
    stred = (l+p) // 2
    if kde[stred] == co:   # Našli jsme
cermak's avatar
Fix  
cermak committed
11
        print(f"Nalezeno na pozici {stred}")
Mareš Martin's avatar
Mareš Martin committed
12 13 14 15 16 17 18
        break
    elif kde[stred] < co:
        l = stred + 1      # Jdeme doprava
    else:
        p = stred - 1      # Jdeme doleva
else:
    print("Kde nic, tu nic")