我试图用不同的排序算法(插入、合并、快速)来测量列表A的排序时间。
错误上说:
AttributeError: 'list' object has no attribute 'first'第5行发生错误
def quick_sort(S):
n = len(S)
if n < 2:
return
p = S.first()
L = LinkedQueue()
E = LInkedQueue()
G = LinkedQueue()
while not S.is_empty():
if S.first() < p:
L.enqueue(S.dequeue())
elif p < S.first():
G.enqueue(S.dequeue())
else:
E.enqueue(S.dequeue())
quick_sort(L)
quick_sort(G)
while not L.is_empty():
S.enqueue(L.dequeue())
while not E.is_empty():
S.enqueue(E.dequeue())
while not G.is_empty():
S.enqueue(G.dequeue())N= 100
从这里开始第五站(array_quick)
array = [random.randint(0, 999999999) for _ in range(n)]
array_quick = array.copy()
start = time.perf_counter()
quick_sort(array_quick)
t_quick = time.perf_counter() - start
if not is_sorted(array_quick):
print("quick_sort: incorrect")
else:
print("quick_sort running time:", t_quick)发布于 2022-04-13 14:32:16
您应该使用S[0]而不是S.first()
发布于 2022-04-13 14:32:30
您是否为列表中的第一个()函数定义了一个方法?
我不认为这是一种固有的列表方法。
这是一张天真的清单给我的:
>>> a = []
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__',
'__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend',
'index', 'insert', 'pop', 'remove', 'reverse', 'sort']也许list对象在不同的项目中定义了这个方法?
class list(list):
def __init__(self, *args):
super().__init__(args)
def first(self):
return self[0]
a = list("item", "another", "info")
print(a.first())https://stackoverflow.com/questions/71859162
复制相似问题