def
binarySearch( theValues, target ) :
# Start with the entire sequence of elements.
low = 0
high = len(theValues) -
1
# Repeatedly subdivide the sequence in half until the target is found.
while
low <= high :
# Find the midpoint of the sequence.
mid = (high + low) // 2
# Does the midpoint contain the target?
if theValues[mid] == target :return True
# Or does the target precede the midpoint?
elif target < theValues[mid] :high = mid - 1
# Or does it follow the midpoint?
else
:
low = mid + 1
# If the sequence cannot be subdivided further, we're done.
return False
本文深入探讨了二分查找算法的核心原理、实现过程及其在不同场景中的应用实例,详细解析了算法的时间复杂度和优化策略。
483

被折叠的 条评论
为什么被折叠?



