看了《编程珠玑》第一部分,完成了二分搜索已经这个函数的测试函数。
二分搜索思想十分简单,但是却很难一次性完成。
所以各种边缘测试是非常重要的。
下面是python完成的二分搜索代码。
def BinarySearch(list, point): list.sort(); lenth = len(list); l = 0; r = len(list) - 1; while(l <= r): m = int((l + r) / 2); temp = list[m]; if (point > temp): l = m + 1; elif (point == temp): return m; elif (point < temp): r = m - 1; return -1;测试函数:def BinarySearchTest(Max): count = 0; for i in range(0,Max): list = []; for j in range(0,i): list.append(random.randint(-Max / 4, Max / 4)) list.sort(); point = random.randint(-25, 25); TestIndex = BinarySearch(list, point); try: RightIndex = list.index(point); except: if (TestIndex == -1): count += 1; pass; else: PRint(list); print('TestIndex = ', TestIndex, 'RightIndex = ', RightIndex) print(point, ':no index error/n'); continue; if (TestIndex != -1): count += 1; pass; else: print(list); print('TestIndex = ', TestIndex, 'RightIndex = ', RightIndex) print(point, ':error/n'); if (count == Max): print('allright'); return count;
新闻热点
疑难解答