我写一个Python程序来测量插入排序的时间复杂度。然而,我在其它{INT(输入)}抓住行号6此错误也出现上述的错误。任何帮助将是巨大的,谢谢。我的代码是:
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()
注意,这些两行代码的:
def input():
和
n=int(input('enter the number of trials'))
在他们的第一个你重新定义了内置函数input()
(它接受0
或1
参数)与自己的相同名称(这仅接受0
参数,即无)。
因此,在他们的第二个你叫不内置功能input()
- 你想要的 - 但你自己一个人,和你1
参数('enter the number of trials'
)callded它,你有相关的错误。
定义你的input()
功能选择一个其他的名字 - 然后使用该名称来调用它,太。