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

1번째 줄: 1번째 줄:
[[분류: 쉘정렬]]
[[분류: 쉘정렬]]
==Java==
[[분류: Java]]
{{참고|Java 쉘정렬 구현}}
<source lang='java'>
public class MyClass {
    static void shellSort(int[] a) {
    int i, j, temp, size = a.length;
    int gap = size / 2;
    while( gap > 0 ) {
        for( i=gap; i<size-1; i++ ) {
            temp = a[i];
            j = i;
            while( j>=gap && a[j-gap]>temp ) {
                a[j] = a[j-gap];
                j -= gap;
            }
            a[j] = temp;
        }
        gap /= 2;
    }
    }
    public static void main(String args[]) {
    int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    shellSort(arr);
    for(int x: arr) System.out.format( "%d ", x );
    // -1 0 1 1 4 9 10 22 22 100
    }
}
</source>
==Perl==
==Perl==
[[분류: Perl]]
[[분류: Perl]]

2018년 8월 28일 (화) 19:47 판


1 Java

public class MyClass {
    static void shellSort(int[] a) {
    	int i, j, temp, size = a.length;
    	int gap = size / 2;
    	while( gap > 0 ) {
    	    for( i=gap; i<size-1; i++ ) {
    	        temp = a[i];
    	        j = i;
    	        while( j>=gap && a[j-gap]>temp ) {
    	            a[j] = a[j-gap];
    	            j -= gap;
    	        }
    	        a[j] = temp;
    	    }
    	    gap /= 2;
    	}
    }
    public static void main(String args[]) {
    	int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    	shellSort(arr);
    	for(int x: arr) System.out.format( "%d ", x );
    	// -1 0 1 1 4 9 10 22 22 100 
    }
}

2 Perl

sub shell_sort {
    my $a=shift;
    $size=@$a;
    $gap=int($size/2);
    while ($gap>0) {
        for $i ($gap..$size-1) {
            $temp=@$a[$i];
            $j=$i;
            while ($j >= $gap && @$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 join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100

3 PHP

<?php
function shell_sort(&$a) {
    $size = count($a);
    $gap = intdiv($size,2);
    while( $gap > 0 ) {
        for($i=$gap; $i<$size; $i++) {
            $temp = $a[$i];
            $j = $i;
            while( $j>=$gap && $a[$j-$gap]>$temp ) {
                $a[$j] = $a[$j-$gap];
                $j -= $gap;
            }
            $a[$j] = $temp;
        }
        $gap = intdiv($gap,2);
    }
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
shell_sort( $arr );
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100

4 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]

5 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]

6 같이 보기

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