2分木探索
## 2分木探索 (Python)
# coding: utf-8
def binary_search(keyword, list1):
# list1 に keyword に合致するものがあるかを 2分木探索で検索する
# list1 をソートする
list1.sort()
# index を設定
index_low = 0
index_high = len(list1) - 1
flag = 0
while index_low <= index_high:
index_mid = index_low + ((index_high - index_low) // 2)
guess = list1[index_mid]
if guess == keyword:
flag = 1
break
elif guess > keyword:
index_high = index_mid - 1
elif guess < keyword:
index_low = index_mid + 1
if flag == 1:
print("exist")
else:
print("not exist")
# 処理開始
list_a = [1, 5, 2, 4, 3]
keyword_a = 30
print(len(list_a))
binary_search(keyword_a, list_a)