创建一个包含20个随机整数的表,并在选项卡中实现二进制搜索算法

2024-06-23 18:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个包含20个随机整数的表,并在python中实现表中的二进制搜索算法。你知道吗

这是我的密码:

def binary_search(a,key):  #function with the binary search algorithm
     imin=0
     imax=le(a)-1
     while (imax >= imin):
         imid=midpoint(imin,imax);
         if (a[imid]==key):
             print(imid)
         elif (a[imid]<key):
             imin=imid+1;
         else:
             imax=imid-1;
     print('KEY_NOT_FOUND')
randomlst=[]                 #a list for saving 20 random numbers
for i in range (20):
    rng=random.randint(1,100)
    randomlst.append(rng)

board=[randolst]             # here i import the list into the board
n=int(input('Give the key')
binary_search(board,n)

我的问题是:

  1. python中的板是否是这样的,如何制作板?你知道吗
  2. 我怎样才能在电路板上添加算法?你知道吗
  3. 我做的二进制搜索算法正确吗?你知道吗

谢谢你的时间,请原谅我的英语。你知道吗


Tags: thekeyboardforsearch二进制randomlist

热门问题