Ruby 삽입정렬 구현

1 개요[편집]

Ruby 삽입정렬 구현
def insertion_sort(a)
    for i in (1...a.size)
        j=i-1
        temp=a[i]
        while j>=0 and a[j]>temp
            a[j+1]=a[j]
            j-=1
        end
        a[j+1]=temp
    end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
insertion_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
def insertion_sort(a)
    for i in (1...a.size)
        j=i
        while j>0 and a[j-1]>a[j]
            a[j-1],a[j] = a[j],a[j-1]
            j -= 1
        end
    end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
insertion_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

2 같이 보기[편집]

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