Ruby 거품정렬 구현

1 개요[ | ]

Ruby 거품정렬 구현
def bubble_sort(a)
    for i in (0...a.size-1)
        for j in (0...a.size-i-1)
            a[j],a[j+1] = a[j+1],a[j] if a[j]>a[j+1]
        end
    end    
end
arr = [9,1,22,4,0,-1,1,22,100,10]
bubble_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
def bubble_sort(a)
    size = a.size-1
    (0..size).each { |i|
        (0...size).each { |j|
            a[j],a[j+1] = a[j+1],a[j] if a[j]>a[j+1]
        }
    }   
end
arr = [9,1,22,4,0,-1,1,22,100,10]
bubble_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

2 같이 보기[ | ]

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