삽입정렬 구현

Jmnote (토론 | 기여)님의 2018년 8월 28일 (화) 20:39 판 (→‎C#)

1 C

16px-Crystal_Clear_app_xmag.svg.png C 삽입정렬 구현 문서를 참고하십시오.
#include <stdio.h>
void insertion_sort(int a[], int size) {
	int i, j, temp;
	for(i=1; i<size; i++) {
		temp = a[i];
		for(j=i-1; j>=0; j--) {
		    if(a[j]<temp) break;
		    a[j+1] = a[j];
		}
		a[j+1] = temp;
	}
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	insertion_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 insertion_sort(int a[], int size) {
	int i, j, temp;
	for(i=1; i<size; i++) {
		temp = a[i];
		for(j=i-1; j>=0; j--) {
		    if(a[j]<temp) break;
		    a[j+1] = a[j];
		}
		a[j+1] = temp;
	}
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	insertion_sort(arr, sizeof(arr)/sizeof(int));
	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 insertionSort(int[] a) {
    	int i, j, temp, size=a.Length;
    	for(i=1; i<size; i++) {
    		temp = a[i];
    		for(j=i-1; j>=0; j--) {
    		    if(a[j]<temp) break;
    		    a[j+1] = a[j];
    		}
    		a[j+1] = temp;
    	}
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        insertionSort(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 insertion_sort(int a[]) {
        int i, j, temp, size = a.length;
        for(i=1; i<size; i++) {
            temp = a[i];
            for(j=i-1; j>=0; j--) {
                if(a[j]<temp) break;
                a[j+1] = a[j];
            }
            a[j+1] = temp;
        }
    }
    public static void main(String args[]) {
    	int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    	insertion_sort(arr);
    	for(int x: arr) System.out.format( "%d ", x );
    	// -1 0 1 1 4 9 10 22 22 100 
    }
}

5 OCaml

let rec isort = function
	| [] -> []
	| h::t -> insert h (isort t)
and insert a = function
	| [] -> [a]
	| h::t when a<h -> [a] @ (h::t)
	| h::t -> [h] @ (insert a t);;

6 Perl

16px-Crystal_Clear_app_xmag.svg.png Perl 삽입정렬 구현 문서를 참고하십시오.
sub insertion_sort {
    my $a=shift;
    $size=@$a;
    for $i (1..$size-1) {
        $j=$i-1;
        $temp=@$a[$i];
        while ($j>=0 && @$a[$j]>$temp) {
            @$a[$j+1]=@$a[$j];
            $j-=1;
        }
        @$a[$j+1]=$temp;
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
insertion_sort(\@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100

7 PHP

16px-Crystal_Clear_app_xmag.svg.png PHP 삽입정렬 구현 문서를 참고하십시오.
<?php
function insertion_sort(&$a) {
    $size = count($a);
    for($i=1; $i<$size; $i++) {
        $temp = $a[$i];
        for($j=$i-1; $j>=0; $j--) {
            if($a[$j]<$temp) break;
            $a[$j+1] = $a[$j];
        }
        $a[$j+1] = $temp;
    }
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
insertion_sort( $arr );
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100

8 Python

16px-Crystal_Clear_app_xmag.svg.png Python 삽입정렬 구현 문서를 참고하십시오.
def insertion_sort(a):
	for i in range(1, len(a)):
		j=i-1
		temp=a[i]
		while j>=0 and a[j]>temp:
			a[j+1]=a[j]
			j-=1
		a[j+1]=temp
arr = [9,1,22,4,0,-1,1,22,100,10]
insertion_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

9 Ruby

16px-Crystal_Clear_app_xmag.svg.png Ruby 삽입정렬 구현 문서를 참고하십시오.
def insertion_sort(a)
    for i in (1...a.size)
        j=i-1
        temp=a[i]
        while j>=0 and a[j]>temp
            a[j+1]=a[j]
            j-=1
        end
        a[j+1]=temp
    end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
insertion_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

10 같이 보기

11 참고

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