쉘정렬 구현


1 C[편집]

16px-Crystal_Clear_app_xmag.svg.png C 쉘정렬 구현 문서를 참고하십시오.
#include <stdio.h>
void shell_sort(int a[], int size) {
    int i, j, temp;
    int gap = 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 /= 2;
    }
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	shell_sort(arr, size);
	for(int i=0; i<size; i++) printf("%d ", arr[i]);
	// -1 0 1 1 4 9 10 22 22 100 
}

2 C++[편집]

16px-Crystal_Clear_app_xmag.svg.png C++ 쉘정렬 구현 문서를 참고하십시오.
#include <iostream>
void shell_sort(int a[], int size) {
    int i, j, temp;
    int gap = 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 /= 2;
    }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    shell_sort(arr, size);
    for(int x: arr) std::cout << x << " ";
    // -1 0 1 1 4 9 10 22 22 100 
}

3 C#[편집]

16px-Crystal_Clear_app_xmag.svg.png C샵 쉘정렬 구현 문서를 참고하십시오.
using System;
class Program {
    static void shellSort(int[] a) {
        int i, j, temp, size=a.Length;
        int gap = 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 /= 2;
        }
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        shellSort(arr);
        Console.Write(string.Join(",",arr));
        // -1,0,1,1,4,9,10,22,22,100
    }
}

4 Java[편집]

16px-Crystal_Clear_app_xmag.svg.png 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; 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 
    }
}

5 Perl[편집]

16px-Crystal_Clear_app_xmag.svg.png 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

6 PHP[편집]

16px-Crystal_Clear_app_xmag.svg.png 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

7 Python[편집]

16px-Crystal_Clear_app_xmag.svg.png 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]

8 Ruby[편집]

16px-Crystal_Clear_app_xmag.svg.png 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]

9 같이 보기[편집]

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