선택정렬 구현

1 C[편집]

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

5 Perl[편집]

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

6 PHP[편집]

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

7 Python[편집]

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

8 Ruby[편집]

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

9 같이 보기[편집]

10 참고[편집]

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