Commit 0511dc7a by BellCodeEditor

auto save

parent b250abfe
Showing with 55 additions and 2 deletions
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file low=0
high=len(num_list)-1
num=20
while low<=high:
mid = (low+high) // 2
guess =num_list[mid]
if guess == num:
print("找到了")
break
elif guess < num:
low = mid + 1
else:
high = mid - 1
print('结束循环')
\ No newline at end of file
import random
num_list=[]
for i in range(101):
num_list.append(i)
def binary_search(list,num):
low=0
high=len(num_list)-1
begin_num=num
while low<=high:
mid = (low+high) // 2
guess =num_list[mid]
if guess == num:
return mid
elif guess < num:
low = mid + 1
else:
high = mid - 1
return None
t=random.choice(num_list)
o=binary_search(num_list,t)
print(o)
\ No newline at end of file
import random
num_list=[]
for i in range(101):
num_list.append(i)
a=[]
while len(a)<31:
s=random.choice(num_list)
if s not in a:
a.append(s)
n=len(a)-1
for j in range(0,n):
for i in range(0,n):
if a[i]>a[i+1]:
a[i],a[i+1]=a[i+1],a[i]
print(a)
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