1 def search(sequence, number, lower = 0, upper = None):
2 if upper is None:
3 upper = len(sequence) - 1
4 if lower == upper:
5 assert(number == sequence[upper])
6 return upper
7 else:
8 middle = (lower + upper) // 2
9 if number > sequence[middle]:
10 return search(sequence, number, middle + 1, upper)
11 else:
12 return search(sequence, number, lower, middle)
13
14 seq = [34, 67, 8, 123, 4, 100, 95]
15 print(seq)
16 seq.sort()
17 print(seq)
18 print(search(seq, 34))
19 print(search(seq, 100))
输出:
[34, 67, 8, 123, 4, 100, 95]
[4, 8, 34, 67, 95, 100, 123]
2
5
posted on 2013-05-16 22:24
unixfy 阅读(329)
评论(0) 编辑 收藏 引用