新學習的計數排序

ZephyrLee發表於2024-07-30
def countSort(arr):

    output = [0 for i in range(256)]

    count = [0 for i in range(256)]

    ans = ["" for _ in arr]

    for i in arr:
        count[ord(i)] += 1

    k = 0
    for i in range(0,256):
        if count[i] == 0:
            continue

        for j in range(count[i]):
            output[k] = chr(i)
            k += 1


    for i in range(len(arr)):
        ans[i] = output[i]

    return ans


arr = "wwwrunoobcom"
print(countSort(arr))

相關文章