파이썬에서 다음과 같은 계산 정렬 알고리즘을 구현했지만 음수가 포함 된 배열에서는 작동하지 않습니다. 누군가 나를 도울 수 있습니까?음수로 작동하는 CountingSort를 구현하려면 어떻게해야합니까?
def counting(nlist):
nlist = list(nlist)
size = len(nlist)
if size < 2:
return nlist
k = max(nlist)
counter = [0] * (k + 1)
for i in nlist:
counter[i] += 1
ndx = 0;
for i in range(len(counter)):
while 0 < counter[i]:
nlist[ndx] = i
ndx += 1
counter[i] -= 1
return nlist
당신은, 당신의 while 루프를 잘못 – samgak
감사를 들여했습니다! – Patterson