我正在编写一个Python程序来测量插入排序的时间复杂度。但是,我在第6行遇到了上述错误。在other {int(inputs)}期间也会发生此错误。谢谢任何帮助。我的代码是:
import random, matplotlib.pyplot as plt
def input():
arr=[]
ret_size=[]
ret_count=[]
n=int(input('enter the number of trials'))
for i in range(n):
x=int(input('size of array:'))
for z in range(x):
r=random.randint(1,2000)
arr.append(r)
count=0
for ind in range(1,len(arr)):
count+=1
cur=arr[ind]
count+=1
pos=ind
while pos>0 and arr[pos-1]>cur:
count+=1
pos=pos-1
count+=1
arr[pos]=cur
count+=1
print('sorted listL')
print(arr)
print('number of hops:')
print(count)
ret_size.append(x)
ret_count.append(count)
plt.plot(ret_size,ret_count)
plt.xlabel('size of input')
plt.ylabel('number of hops')
plt.title('insertion sort')
plt.show()
input()
答案 0 :(得分:0)
请注意以下两行代码:
def input():
和
n=int(input('enter the number of trials'))
在其中第一个中,您使用用自己的函数重新定义了内置函数input()
(接受0
或1
参数) >相同的名称(仅接受 0
个参数,即不接受)。
因此,在第二个中,您根据需要-调用了内置函数input()
, NOT ,但您自己的它与1
参数('enter the number of trials'
)配合使用,您会遇到相关错误。
选择用于定义您的 input()
函数的其他名称-然后也使用该名称进行调用。