병합정렬 구현

Jmnote (토론 | 기여)님의 2018년 8월 27일 (월) 19:58 판 (→‎C++)

1 C

16px-Crystal_Clear_app_xmag.svg.png C 병합정렬 구현 문서를 참고하십시오.
#include <stdio.h>
void merge_sort(int a[], int left, int right) {
    if( left>=right ) return;
    int middle = (left+right-1)/2;
    merge_sort(a, left, middle);
    merge_sort(a, middle+1, right);
    int n1 = middle + 1 - left;
    int n2 = right - middle;
    int i, j, k, L[n1], R[n2];
    for(i=0; i<n1; i++) L[i]=a[left+i];
    for(i=0; i<n2; i++) R[i]=a[middle+1+i];
    i=j=0; k=left;
    while( i<n1 && j<n2 ) {
        if( L[i]<=R[j] ) { a[k]=L[i]; i++; k++; }
        else { a[k]=R[j]; j++; k++; }
    }
    while( i<n1 ) { a[k]=L[i]; i++; k++; }
    while( j<n2 ) { a[k]=R[j]; j++; k++; }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,100};
    int size = sizeof(arr)/sizeof(int);
    merge_sort(arr, 0, size-1);
    for(int i=0; i<size; i++) printf("%d ", arr[i]);
    // -1 0 1 1 4 9 22 22 100 100 
}

2 C++

16px-Crystal_Clear_app_xmag.svg.png C++ 병합정렬 구현 문서를 참고하십시오.
#include <iostream>
void merge_sort(int a[], int left, int right) {
    if( left>=right ) return;
    int middle = (left+right-1)/2;
    merge_sort(a, left, middle);
    merge_sort(a, middle+1, right);
    int n1 = middle + 1 - left;
    int n2 = right - middle;
    int i, j, k, L[n1], R[n2];
    for(i=0; i<n1; i++) L[i]=a[left+i];
    for(i=0; i<n2; i++) R[i]=a[middle+1+i];
    i=j=0; k=left;
    while( i<n1 && j<n2 ) {
        if( L[i]<=R[j] ) { a[k]=L[i]; i++; k++; }
        else { a[k]=R[j]; j++; k++; }
    }
    while( i<n1 ) { a[k]=L[i]; i++; k++; }
    while( j<n2 ) { a[k]=R[j]; j++; k++; }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    merge_sort(arr, 0, size-1);
    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 merge_sort(int[] a) {
        merge_sort(a,0,a.Length-1);
    }
    static void merge_sort(int[] a, int left, int right) {
        if( left>=right ) return;
        int middle = (left+right-1)/2;
        merge_sort(a, left, middle);
        merge_sort(a, middle+1, right);
        int n1 = middle + 1 - left;
        int n2 = right - middle;
        int[] L=new int[n1], R=new int[n2];
        int i, j, k;
        for(i=0; i<n1; i++) L[i]=a[left+i];
        for(i=0; i<n2; i++) R[i]=a[middle+1+i];
        i=j=0; k=left;
        while( i<n1 && j<n2 ) {
            if( L[i]<=R[j] ) { a[k]=L[i]; i++; k++; }
            else { a[k]=R[j]; j++; k++; }
        }
        while( i<n1 ) { a[k]=L[i]; i++; k++; }
        while( j<n2 ) { a[k]=R[j]; j++; k++; }
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        merge_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 merge(int[] arr, int left, int middle, int right) {
        int n1 = middle - left + 1;
        int n2 = right - middle;
        int L[] = new int[n1];
        int R[] = new int[n2];
        int i, j, k;
        for(i=0; i<n1; ++i) L[i]=arr[left+i];
        for(j=0; j<n2; ++j) R[j]=arr[middle+j+1];
        i=0; j=0; k=left;
        while( i<n1 && j<n2) {
            if(L[i] <= R[j]) { arr[k] = L[i]; i++; }
            else { arr[k] = R[j]; j++; }
            k++;
        }
        while( i < n1 ) { arr[k] = L[i]; i++; k++; }
        while( j < n2 ) { arr[k] = R[j]; j++; k++; }
    }
    static void merge_sort(int arr[], int left, int right) {
        if(left >= right) return;
        int middle = (left+right)/2;
        merge_sort(arr, left, middle);
        merge_sort(arr , middle+1, right);
        merge(arr, left, middle, right);
    }
    public static void main(String args[]) {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        merge_sort(arr, 0, arr.length-1);
        for(int x: arr) System.out.format("%d ",x);
        // -1 0 1 1 4 9 10 22 22 100 
    }
}

5 PHP

16px-Crystal_Clear_app_xmag.svg.png PHP 병합정렬 구현 문서를 참고하십시오.
<?php
function merge_sort(&$a, $left=0, $right=null) {
    if( is_null($right) ) $right=count($a)-1;
    if( $left>=$right ) return;
    $middle = intdiv($left+$right-1, 2);
    merge_sort($a, $left, $middle);
    merge_sort($a, $middle+1, $right);
    $n1 = $middle + 1 - $left;
    $n2 = $right - $middle;
    $L = array_fill(0, $n1, 0);
    $R = array_fill(0, $n2, 0);
    for($i=0; $i<$n1; $i++) $L[$i] = $a[$left+$i];
    for($i=0; $i<$n2; $i++) $R[$i] = $a[$middle+1+$i];
    $k = $left;
    $i = $j = 0;
    while( $i<$n1 && $j<$n2 ) {
        if( $L[$i]<=$R[$j] ) { $a[$k] = $L[$i]; $i++; $k++; }
        else { $a[$k] = $R[$j]; $j++; $k++; }
    }
    while( $i<$n1 ) { $a[$k]=$L[$i]; $i++; $k++; }
    while( $j<$n2 ) { $a[$k]=$R[$j]; $j++; $k++; }
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
merge_sort( $arr );
echo implode(' ',$arr);
# -1 0 1 1 4 9 10 22 22 100

6 Python

16px-Crystal_Clear_app_xmag.svg.png Python 병합정렬 구현 문서를 참고하십시오.
def merge_sort(a, left=0, right=-1):
    def merge(a, left, middle, right):
    	n1 = middle + 1 - left 
    	n2 = right - middle
    	L = [0] * (n1)
    	R = [0] * (n2)
    	for i in range(0, n1): L[i]=a[left+i]
    	for j in range(0, n2): R[j]=a[middle+1+j]
    	i = j = 0
    	k = left
    	while i<n1 and j<n2 :
    		if L[i]<=R[j]:
    		    a[k]=L[i]; i+=1; k+=1
    		else:
    		    a[k]=R[j]; j+=1; k+=1
    	while i<n1: a[k]=L[i]; i+=1; k+=1
    	while j<n2:	a[k]=R[j]; j+=1; k+=1
    if right == -1: right = len(a)-1
    if left >= right: return
    middle = int((left+(right-1))/2)
    merge_sort(a, left, middle)
    merge_sort(a, middle+1, right)
    merge(a, left, middle, right)
arr = [9,1,22,4,0,-1,1,22,100,10]
merge_sort(arr)
print( arr )
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]

7 같이 보기

8 참고

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