Skip to content
Snippets Groups Projects
Commit eedebf19 authored by Martin Mareš's avatar Martin Mareš
Browse files

Rozděl a panuj: Binární vyhledávání

parent 808ab5a6
Branches
No related tags found
No related merge requests found
#!/usr/bin/python3
# Binární vyhledávání
def hledej(A, x):
# Hledá x v poli A, vrací buď pozici, nebo None
def bin(l, r):
# Hledá x v úseku A[l...r]
if l > r:
return None
s = (l+r) // 2
if x < A[s]:
return bin(l, s-1)
elif x > A[s]:
return bin(s+1, r)
else:
return s
return bin(0, len(A)-1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment