Ruby 기수정렬 구현

1 개요[ | ]

Ruby 기수정렬 구현
루비 기수정렬 구현
def radix_sort(a, base=10)
    size = a.size
    maxval = a.max()
    ex = 1
    while ex <= maxval
        output = Array.new(size,0)
        count = Array.new(base,0)
        for i in (0...size); count[(a[i]/ex)%base]+=1; end
        for i in (1...base); count[i]+=count[i-1]; end
        for i in (size-1).downto(0)
        	j = (a[i]/ex)%base
        	output[count[j]-1] = a[i]
        	count[j] -= 1
        end
        for i in (0...size); a[i]=output[i]; end
        ex *= base
    end
end
arr = [9,1,22,4,0,1,22,100,10]
radix_sort(arr)
print arr
# [0, 1, 1, 4, 9, 10, 22, 22, 100]

2 같이 보기[ | ]

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