"빗질정렬 구현"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 10개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류:빗질정렬]]
[[분류:빗질정렬]]
==C==
[[분류: C]]
{{참고|C언어 빗질정렬 구현}}
<syntaxhighlight lang='c'>
#include <stdio.h>
void comb_sort(int a[], int size) {
    int i, temp, gap;
    int swapped = 1;
    while( gap!=1 || swapped ) {
        gap = gap*10/13;
        if( gap<1 ) gap=1;
        swapped = 0;
        for( i=0; i<size-gap; i++ ) {
            if( a[i] > a[i+gap] ) {
                temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                swapped = 1;
            }
        }
    }
}
int main() {
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
int size = sizeof(arr)/sizeof(int);
comb_sort(arr, size);
for(int i=0; i<size; i++) printf("%d ", arr[i]);
// -1 0 1 1 4 9 10 22 22 100
}
</syntaxhighlight>
==C++==
[[분류: C++]]
{{참고|C++ 빗질정렬 구현}}
<syntaxhighlight lang='cpp'>
#include <iostream>
void comb_sort(int a[], int size) {
    int i, temp, gap;
    bool swapped = true;
    while( gap!=1 || swapped ) {
        gap = gap*10/13;
        if( gap<1 ) gap=1;
        swapped = false;
        for( i=0; i<size-gap; i++ ) {
            if( a[i] > a[i+gap] ) {
                temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                swapped = true;
            }
        }
    }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    comb_sort(arr, size);
    for(int x: arr) std::cout << x << " ";
    // -1 0 1 1 4 9 10 22 22 100
}
</syntaxhighlight>
==C#==
[[분류: csharp]]
{{참고|C샵 빗질정렬 구현}}
<syntaxhighlight lang='csharp'>
using System;
class Program {
    static void combSort(int[] a) {
        int i, temp, gap, size;
        gap = size = a.Length;
        bool swapped = true;
        while( gap!=1 || swapped ) {
            gap = gap*10/13;
            if( gap<1 ) gap=1;
            swapped = false;
            for( i=0; i<size-gap; i++ ) {
                if( a[i] > a[i+gap] ) {
                    temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                    swapped = true;
                }
            }
        }
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        combSort( arr );
        Console.Write(string.Join(",",arr));
        // -1,0,1,1,4,9,10,22,22,100 
    }
}
</syntaxhighlight>
==Java==
[[분류: java]]
{{참고|Java 빗질정렬 구현}}
<syntaxhighlight lang='java'>
public class MyClass {
    static void combSort(int[] a) {
    int i, temp, gap, size;
    gap = size = a.length;
    boolean swapped = true;
    while( gap!=1 || swapped ) {
        gap = gap*10/13;
        if( gap<1 ) gap=1;
        swapped = false;
        for( i=0; i<size-gap; i++ ) {
            if( a[i] > a[i+gap] ) {
                temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                    swapped = true;
            }
        }
    }
    }
    public static void main(String args[]) {
    int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    combSort(arr);
    for(int x: arr) System.out.format( "%d ", x );
    // -1 0 1 1 4 9 10 22 22 100
    }
}
</syntaxhighlight>
==Perl==
==Perl==
[[분류:Perl]]
[[분류:Perl]]
<source lang='perl'>
{{참고|Perl 빗질정렬 구현}}
<syntaxhighlight lang='perl'>
sub comb_sort {
    my $a=shift;
    $gap=$size=@$a;
    $swapped=1;
    while ($gap ne 1 || $swapped) {
        $gap = int(($gap*10)/13);
        $gap=1 if ($gap<1);
        $swapped=0;
        for $i (0..$size-$gap-1) {
            if (@$a[$i]>@$a[$i+$gap]) {
                (@$a[$i],@$a[$i+$gap]) = (@$a[$i+$gap],@$a[$i]);
                $swapped=1;
            }
        }
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
@arr = (9,1,22,4,0,-1,1,22,100,10);
$gap=$size=@arr;
comb_sort(\@arr);
$swapped=1;
print join(',',@arr);
while ($gap ne 1 || $swapped) {
# -1,0,1,1,4,9,10,22,22,100
         $gap = int(($gap*10)/13);
</syntaxhighlight>
         $gap = 1 if ($gap<1);
 
         $swapped = 0;
==PHP==
         for $i (0..$size-$gap) {
[[분류: PHP]]
                if ($arr[$i]>$arr[$i+$gap]) {
{{참고|PHP 빗질정렬 구현}}
                        ($arr[$i], $arr[$i+$gap]) = ($arr[$i+$gap], $arr[$i]);
<syntaxhighlight lang='php'>
                        $swapped=1;
<?php
                }
function comb_sort(&$a) {
    $gap = $size = count($a);
    $swapped = true;
    while( $gap!=1 || $swapped ) {
         $gap = intdiv($gap*10, 13);
         if( $gap<1 ) $gap=1;
         $swapped = false;
         for( $i=0; $i<$size-$gap; $i++) {
            if( $a[$i] > $a[$i+$gap] ) {
                $temp=$a[$i]; $a[$i]=$a[$i+$gap]; $a[$i+$gap]=$temp;
                $swapped = true;
            }
         }
         }
    }
}
}
print join(',', grep /\S/, @arr);
$arr = [9,1,22,4,0,-1,1,22,100,10];
#-1,0,1,1,4,9,10,22,22,100
comb_sort( $arr );
</source>
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100
</syntaxhighlight>


==Python==
==Python==
[[분류: Python]]
[[분류: Python]]
{{참고|Python 빗질정렬 구현}}
{{참고|Python 빗질정렬 구현}}
<source lang='python'>
<syntaxhighlight lang='python'>
def comb_sort(a):
def comb_sort(a):
     gap=size=len(a)
     gap=size=len(a)
40번째 줄: 191번째 줄:
print( arr )
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
</source>
</syntaxhighlight>
 
==Ruby==
[[분류: Ruby]]
{{참고|Ruby 빗질정렬 구현}}
<syntaxhighlight lang='ruby'>
def comb_sort(a)
    gap=size=a.size
    swapped = true
    while gap !=1 or swapped
        gap = gap*10/13
        if gap<1; gap=1; end
        swapped = false
        for i in (0...size-gap)
            if a[i]>a[i+gap]
                a[i],a[i+gap] = a[i+gap],a[i]
                swapped = true
            end
        end
    end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
comb_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[빗질정렬]]
* [[빗질정렬]]
* [[정렬 구현]]
* [[정렬 구현]]

2020년 11월 2일 (월) 02:31 기준 최신판


1 C[ | ]

#include <stdio.h>
void comb_sort(int a[], int size) {
    int i, temp, gap;
    int swapped = 1;
    while( gap!=1 || swapped ) {
        gap = gap*10/13;
        if( gap<1 ) gap=1;
        swapped = 0;
        for( i=0; i<size-gap; i++ ) {
            if( a[i] > a[i+gap] ) {
                temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                swapped = 1;
            }
        }
    }
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	comb_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 comb_sort(int a[], int size) {
    int i, temp, gap;
    bool swapped = true;
    while( gap!=1 || swapped ) {
        gap = gap*10/13;
        if( gap<1 ) gap=1;
        swapped = false;
        for( i=0; i<size-gap; i++ ) {
            if( a[i] > a[i+gap] ) {
                temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                swapped = true;
            }
        }
    }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    comb_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 combSort(int[] a) {
        int i, temp, gap, size;
        gap = size = a.Length;
        bool swapped = true;
        while( gap!=1 || swapped ) {
            gap = gap*10/13;
            if( gap<1 ) gap=1;
            swapped = false;
            for( i=0; i<size-gap; i++ ) {
                if( a[i] > a[i+gap] ) {
                    temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                    swapped = true;
                }
            }
        }
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        combSort( arr );
        Console.Write(string.Join(",",arr));
        // -1,0,1,1,4,9,10,22,22,100  
    }
}

4 Java[ | ]

public class MyClass {
    static void combSort(int[] a) {
    	int i, temp, gap, size;
    	gap = size = a.length;
    	boolean swapped = true;
    	while( gap!=1 || swapped ) {
    	    gap = gap*10/13;
    	    if( gap<1 ) gap=1;
    	    swapped = false;
    	    for( i=0; i<size-gap; i++ ) {
    	        if( a[i] > a[i+gap] ) {
    	            temp=a[i]; a[i]=a[i+gap]; a[i+gap]=temp;
                    swapped = true;
    	        }
    	    }
    	}
    }
    public static void main(String args[]) {
    	int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    	combSort(arr);
    	for(int x: arr) System.out.format( "%d ", x );
    	// -1 0 1 1 4 9 10 22 22 100 
    }
}

5 Perl[ | ]

sub comb_sort {
    my $a=shift;
    $gap=$size=@$a;
    $swapped=1;
    while ($gap ne 1 || $swapped) {
        $gap = int(($gap*10)/13);
        $gap=1 if ($gap<1);
        $swapped=0;
        for $i (0..$size-$gap-1) {
            if (@$a[$i]>@$a[$i+$gap]) {
                (@$a[$i],@$a[$i+$gap]) = (@$a[$i+$gap],@$a[$i]);
                $swapped=1;
            }
        }
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
comb_sort(\@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100

6 PHP[ | ]

<?php
function comb_sort(&$a) {
    $gap = $size = count($a);
    $swapped = true;
    while( $gap!=1 || $swapped ) {
        $gap = intdiv($gap*10, 13);
        if( $gap<1 ) $gap=1;
        $swapped = false;
        for( $i=0; $i<$size-$gap; $i++) {
            if( $a[$i] > $a[$i+$gap] ) {
                $temp=$a[$i]; $a[$i]=$a[$i+$gap]; $a[$i+$gap]=$temp;
                $swapped = true;
            }
        }
    }
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
comb_sort( $arr );
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100

7 Python[ | ]

def comb_sort(a):
    gap=size=len(a)
    swapped = True
    while gap !=1 or swapped:
        gap = (gap*10)//13
        if gap<1: gap=1
        swapped = False
        for i in range(0, size-gap):
            if a[i]>a[i+gap]:
                a[i],a[i+gap] = a[i+gap],a[i]
                swapped = True
arr = [9,1,22,4,0,-1,1,22,100,10]
comb_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

8 Ruby[ | ]

def comb_sort(a)
    gap=size=a.size
    swapped = true
    while gap !=1 or swapped
        gap = gap*10/13
        if gap<1; gap=1; end
        swapped = false
        for i in (0...size-gap)
            if a[i]>a[i+gap]
                a[i],a[i+gap] = a[i+gap],a[i]
                swapped = true
            end
        end
    end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
comb_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

9 같이 보기[ | ]

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