Ruby 퀵정렬 구현

1 개요[ | ]

Ruby 퀵정렬 구현
루비 퀵정렬 구현
def quick_sort(a, l=0, r=-1)
    if r==-1; r=a.size-1; end
    pivot=a[l]
    left=l
    right=r
    while left<right
        while a[right]>=pivot and left<right; right-=1; end
        if left<right; a[left]=a[right]; end
        while a[left]<=pivot and left<right; left+=1; end
        if left>=right; break; end
        a[right]=a[left]
        right-=1
    end
    a[left]=pivot
    if left>l; quick_sort(a, l, left-1); end
    if left<r; quick_sort(a, left+1, r); end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
quick_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

2 같이 보기[ | ]

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