"쉘정렬 구현"의 두 판 사이의 차이

31번째 줄: 31번째 줄:
     while gap>0
     while gap>0
         for i in (gap...size)
         for i in (gap...size)
            temp=a[i]
             j=i
             j=i
             while j>=gap and a[j-gap]>a[j]
             while j>=gap and a[j-gap]>temp
                 a[j-gap],a[j] = a[j],a[j-gap]
                 a[j]=a[j-gap]
                 j -= gap
                 j-=gap
             end
             end
            a[j]=temp
         end
         end
         gap /= 2
         gap /= 2

2018년 8월 28일 (화) 03:38 판

1 Python

def shell_sort(a):
    size = len(a)
    gap = size//2
    while gap>0:
        for i in range(gap,size):
            temp=a[i]
            j=i
            while j>=gap and a[j-gap]>temp:
                a[j]=a[j-gap]
                j-=gap
            a[j]=temp
        gap//=2
arr = [9,1,22,4,0,-1,1,22,100,10]
shell_sort(arr)
print(arr)
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

2 Ruby

def shell_sort(a)
    size = a.size
    gap = size/2
    while gap>0
        for i in (gap...size)
            temp=a[i]
            j=i
            while j>=gap and a[j-gap]>temp
                a[j]=a[j-gap]
                j-=gap
            end
            a[j]=temp
        end
        gap /= 2
    end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
shell_sort(arr)
print(arr)
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

3 같이 보기

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