python basis - binary search

# Binary search 


DEF sort_search (LST, Key):
"" "
binary search
: param lst: an ordered sequence
: param key: the key value you're looking for
: return: key indexes in the number of columns in
" ""
Low, High = 0, len (LST) -1
the while Low <High:
MID = (High + Low) // 2
IF LST [MID] <Key:
Low = MID +. 1
elif LST [MID]> Key:
High = MID -. 1
the else:
return MID
return None


L = [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9]
Print ( 'Key index:', sort_search (L, 80))

Guess you like

Origin www.cnblogs.com/jeffrey04118110/p/11801665.html