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

(새 문서: 분류: 쉘정렬 ==Python== 분류: Python {{참고|Python 쉘정렬 구현}} <source lang='python'> def shell_sort(a): size = len(a) gap = size//2 while gap>0:...)
 
(사용자 2명의 중간 판 15개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: 쉘정렬]]
[[분류: 쉘정렬]]
==C==
[[분류: C]]
{{참고|C 쉘정렬 구현}}
<source lang='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
}
</source>
==C++==
[[분류:C++]]
{{참고|C++ 쉘정렬 구현}}
<source lang='cpp'>
#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
}
</source>
==C#==
[[분류:csharp]]
{{참고|C샵 쉘정렬 구현}}
<source lang='csharp'>
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
    }
}
</source>
==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; 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 쉘정렬 구현}}
<source lang='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
</source>
==PHP==
[[분류: PHP]]
{{참고|PHP 쉘정렬 구현}}
<source lang='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
</source>
==Python==
==Python==
[[분류: Python]]
[[분류: Python]]
21번째 줄: 197번째 줄:
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
</source>
</source>
==Ruby==
[[분류: Ruby]]
{{참고|Ruby 쉘정렬 구현}}
<source lang='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]
</source>
==같이 보기==
* [[쉘정렬]]
* [[정렬 구현]]

2018년 8월 31일 (금) 21:02 판


1 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++

#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#

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

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

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

<?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

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

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 }}