Ruby 계수정렬 구현

1 개요[ | ]

Ruby 계수정렬 구현
루비 계수정렬 구현
def counting_sort(a)
    count={}
    for x in a
        if not count.key?(x) then count[x]=1
        else count[x]+=1 end
    end
    pos = 0
    for x,repeat in count.sort_by {|k,v| k}
        a[pos,repeat] = Array.new(repeat,x)
        pos += repeat
    end
end
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 }}