[CLR][Ruby] Counting Sort 공부이야기

'Introduction to Algorithms(CLR)' 8장에 나오는 계수 정렬(Counting Sort)를 루비로 구현해 봤다.

def counting_sort(input, output, k)
  temp = Array.new(k+10)
  length = input.size - 1

  (0..length).each {| i | temp[input[i]] = temp[input[i]] + 1}
  (1..k).each {| i | temp[i] = temp[i] + temp[i - 1]}
  length.downto(0do | i |
    output[temp[input[i]]] = input[i]
    temp[input[i]] = temp[input[i]] - 1
  end

  output
end

a = [6021346132]
b = Array.new
puts counting_sort(a, b, a.max).inspect

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://grow.egloos.com/tb/4816694 [도움말]

덧글

덧글 입력 영역