"삽입정렬 구현"의 두 판 사이의 차이

(Python)
110번째 줄: 110번째 줄:
 
| h::t when a<h -> [a] @ (h::t)
 
| h::t when a<h -> [a] @ (h::t)
 
| h::t -> [h] @ (insert a t);;
 
| h::t -> [h] @ (insert a t);;
 +
</source>
 +
 +
==Perl==
 +
[[분류:Perl]]
 +
<source lang='perl'>
 +
@arr = (9,1,22,4,0,-1,1,22,100,10);
 +
$size=@arr;
 +
for $i (1..$size) {
 +
    $j=$i-1;
 +
    $temp=$arr[$i];
 +
    while ($j>=0 && $arr[$j]>$temp) {
 +
        $arr[$j+1]=$arr[$j];
 +
        $j-=1;
 +
    }
 +
    $arr[$j+1]=$temp;
 +
}
 +
print join(',', grep /\S/, @arr);
 +
#-1,0,1,1,4,9,10,22,22,100
 
</source>
 
</source>
  

2018년 8월 28일 (화) 09:34 판

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 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;
    	}
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        insertion_sort(arr);
        foreach(int i in arr) Console.Write(i+" ");
        // -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

@arr = (9,1,22,4,0,-1,1,22,100,10);
$size=@arr;
for $i (1..$size) {
    $j=$i-1;
    $temp=$arr[$i];
    while ($j>=0 && $arr[$j]>$temp) {
        $arr[$j+1]=$arr[$j];
        $j-=1;
    }
    $arr[$j+1]=$temp;
}
print join(',', grep /\S/, @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 }}