Python 计数排序
减少循环次数参考:
def countSort(arr):
count =[0for i in range(256)]
output =[""for _ in arr]
mx, mn =0,255
for i in arr:
idx = ord(i)
if idx > mx: mx = idx
if idx < mn: mn = idx
count[idx]+=1
idx =0
for i in range(mn, mx +1):
if count[i]==0:continue
for _ in range(count[i]):
output[idx]= chr(i)
idx +=1
return output
arr ="wwwrunoobcom"
ans = countSort(arr)
print("字符数组排序 %s" %("".join(ans)))