Counting Sort

jeongwon yun·2022년 10월 16일
0

Algorithm

목록 보기
2/18
arr = [5, 3, 2, 1, 1, 2, 4, 0, 3]
counts = [0] * 100

for i in range(len(arr)):
    counts[arr[i]] += 1

for i in range(1, len(counts)):
    counts[i] += counts[i - 1] # 누적 합

sorted = [-1] * len(arr)

for i in range(len(sorted) - 1, -1, -1):
    counts[arr[i]] -= 1
    sorted[counts[arr[i]]] = arr[i]

print(sorted) # [0, 1, 1, 2, 2, 3, 3, 4, 5]

0개의 댓글