Commit 44a3c2fd by BellCodeEditor

save project

parent 136bb95f
Showing with 12 additions and 16 deletions
# 使用二分查找法,找出9和20在列表里面的索引 import turtle
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] a = turtle.Pen()
a.pencolor("red")
def binary_search(alist,num): a.pensize(5)
low = 0 a.left(45)
high = len(alist)-1 a.forward(100)
while low <= high: a.circle(50,180)
mid = (low + high) // 2 a.right(90)
guess = alist[mid] a.circle(50,180)
if guess == num: a.forward(100)
return mid turtle.done()
elif guess < num: \ No newline at end of file
low = mid + 1
else:
high = mid - 1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment