선택정렬 구현

100.96.1.249 (토론)님의 2019년 7월 23일 (화) 17:57 판 (→‎Python)

1 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++

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

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

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

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

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

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

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