Python 계수정렬 구현

1 개요[ | ]

Python 카운팅정렬 구현
def counting_sort(a):
	count={}
	for x in a: count[x]=count.get(x,0)+1
	pos=0
	for x,repeat in sorted(count.items()):
	    a[pos:pos+repeat] = [x]*repeat
	    pos += repeat
arr = [9,1,22,4,0,-1,1,22,100,10]
counting_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
def counting_sort(a):
    minval = min(a)
    maxval = max(a)
    count = {}
    for i in range(minval,maxval+1): count[i]=0
    for x in a: count[x]+=1
    i = 0
    for x, c in count.items():
        if c==0: continue
        a[i:i+c] = [x]*c
        i += c
arr = [9,1,22,4,0,-1,1,22,100,10]
counting_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}