MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / binarySearch

Function binarySearch

BinarySearch.py:4–17  ·  view source on GitHub ↗
(alist, item)

Source from the content-addressed store, hash-verified

2# 输入:一个顺序list
3# 输出: 待查找的元素的位置
4def binarySearch(alist, item):
5 first = 0
6 last = len(alist) - 1
7
8 while first <= last:
9 mid = (first + last)//2
10 print(mid)
11 if alist[mid] > item:
12 last = mid - 1
13 elif alist[mid] < item:
14 first = mid + 1
15 else:
16 return mid+1
17 return -1
18
19test = [0, 1, 2, 8, 13, 17, 19, 32, 42]
20print(binarySearch(test, 3))

Callers 1

BinarySearch.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected