聚会游戏,一个人想一个数,其他人来猜,然后告诉你猜大了还是小了,直到猜到这个数。
二分法和猜数游戏类似,只不过猜的时候一定猜最中间的那个数,折半查找所需内容,就数组来说,数组越长,梯度下降越快,二分查找优势越明显。
python代码如下:
1 def binary_search(list, item): 2 low = 0 3 high = len(list)-1 # Tracking list head and tail 4 n = 0 5 while low <= high: 6 mid = int((low + high)/2)