빗질정렬 구현


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