"병합정렬 구현"의 두 판 사이의 차이

(C)
(C)
8번째 줄: 8번째 줄:
  
 
int a[1000000];
 
int a[1000000];
 
 
void Merge(int arr[], int StartInd, int MiddleInd, int EndInd){
 
void Merge(int arr[], int StartInd, int MiddleInd, int EndInd){
 
 
     int FirInd = StartInd, SecInd = MiddleInd+1, ResInd=0;
 
     int FirInd = StartInd, SecInd = MiddleInd+1, ResInd=0;
 
 
     int * Res = (int*)malloc(sizeof(int) * (EndInd - StartInd+1));
 
     int * Res = (int*)malloc(sizeof(int) * (EndInd - StartInd+1));
 
 
     while(FirInd <= MiddleInd && SecInd <= EndInd){
 
     while(FirInd <= MiddleInd && SecInd <= EndInd){
 
         if(arr[FirInd]<arr[SecInd]){ // 오름차순 일 경우
 
         if(arr[FirInd]<arr[SecInd]){ // 오름차순 일 경우
23번째 줄: 19번째 줄:
 
         }
 
         }
 
     }
 
     }
 
 
     while(FirInd<=MiddleInd){
 
     while(FirInd<=MiddleInd){
 
         Res[ResInd++]=arr[FirInd++];
 
         Res[ResInd++]=arr[FirInd++];
 
     }
 
     }
 
 
     while(SecInd<=EndInd){
 
     while(SecInd<=EndInd){
 
         Res[ResInd++]=arr[SecInd++];
 
         Res[ResInd++]=arr[SecInd++];
 
     }
 
     }
 
 
     ResInd = 0;
 
     ResInd = 0;
 
     for(int i = StartInd;i<=EndInd;i++){
 
     for(int i = StartInd;i<=EndInd;i++){
38번째 줄: 31번째 줄:
 
     free(Res);
 
     free(Res);
 
}
 
}
 
 
void MergeSort(int arr[], int StartInd, int EndInd){
 
void MergeSort(int arr[], int StartInd, int EndInd){
 
 
     int MiddleInd;
 
     int MiddleInd;
 
 
     if(EndInd - StartInd <1)return;
 
     if(EndInd - StartInd <1)return;
 
 
     MiddleInd=(StartInd + EndInd)/2;
 
     MiddleInd=(StartInd + EndInd)/2;
 
     MergeSort(arr,StartInd,MiddleInd);
 
     MergeSort(arr,StartInd,MiddleInd);
 
     MergeSort(arr,MiddleInd + 1,EndInd);
 
     MergeSort(arr,MiddleInd + 1,EndInd);
 
 
     Merge(arr,StartInd,MiddleInd,EndInd);
 
     Merge(arr,StartInd,MiddleInd,EndInd);
 
 
}
 
}
 
 
 
 
int main(){
 
int main(){
 
 
     int n;
 
     int n;
 
     scanf("%d",&n);
 
     scanf("%d",&n);

2019년 3월 13일 (수) 23:33 판

1 C

16px-Crystal_Clear_app_xmag.svg.png C 병합정렬 구현 문서를 참고하십시오.
#include <stdio.h>
#include <stdlib.h>

int a[1000000];
void Merge(int arr[], int StartInd, int MiddleInd, int EndInd){
    int FirInd = StartInd, SecInd = MiddleInd+1, ResInd=0;
    int * Res = (int*)malloc(sizeof(int) * (EndInd - StartInd+1));
    while(FirInd <= MiddleInd && SecInd <= EndInd){
        if(arr[FirInd]<arr[SecInd]){ // 오름차순 일 경우
            Res[ResInd++]=arr[FirInd++];
        }
        else{
            Res[ResInd++]=arr[SecInd++];
        }
    }
    while(FirInd<=MiddleInd){
        Res[ResInd++]=arr[FirInd++];
    }
    while(SecInd<=EndInd){
        Res[ResInd++]=arr[SecInd++];
    }
    ResInd = 0;
    for(int i = StartInd;i<=EndInd;i++){
        arr[i]=Res[ResInd++];
    }
    free(Res);
}
void MergeSort(int arr[], int StartInd, int EndInd){
    int MiddleInd;
    if(EndInd - StartInd <1)return;
    MiddleInd=(StartInd + EndInd)/2;
    MergeSort(arr,StartInd,MiddleInd);
    MergeSort(arr,MiddleInd + 1,EndInd);
    Merge(arr,StartInd,MiddleInd,EndInd);
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    MergeSort(a,0,n-1);
    for(int i=0;i<n;i++){
        printf("%d\n",a[i]);
    }
    return 0;
}

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);
        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 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 Perl

16px-Crystal_Clear_app_xmag.svg.png Perl 병합정렬 구현 문서를 참고하십시오.
sub merge_sort {
    my ($a) = @_;
    $size = @$a;
    return if $size < 2;
    $bound = int $size/2;
    my @L = @$a[0 .. $bound-1];
    my @R = @$a[$bound .. $size-1];
    merge_sort( \@L );
    merge_sort( \@R );
    for $i (@$a) {
        $i = !@R ? shift @L : !@L ? shift @R
            : @R[0]<@L[0] ? shift @R : shift @L;
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
merge_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 merge_sort(&$a) {
    $size = count($a);
    if( $size<2 ) return;
    $bound = intdiv($size,2);
    $L = array_slice($a, 0, $bound);
    $R = array_slice($a, $bound, $size);
    merge_sort( $L );
    merge_sort( $R );
    foreach( $a as &$x ) {
        if(empty($R)) { $x=array_shift($L); continue; }
        if(empty($L)) { $x=array_shift($R); continue; }
        $x = ($R[0]<$L[0])? array_shift($R): array_shift($L);
    }
}
$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

7 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 = (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]

8 Ruby

16px-Crystal_Clear_app_xmag.svg.png Ruby 병합정렬 구현 문서를 참고하십시오.
def merge_sort(a, left=0, right=-1)
    def merge(a, left, middle, right)
        n1 = middle+1-left
        n2 = right-middle
        l = Array.new(n1,0)
        r = Array.new(n2,0)
        for i in (0...n1); l[i]=a[left+i]; end
        for i in (0...n2); r[i]=a[middle+1+i]; end
        i=j=0; k=left
        while i<n1 and j<n2
            if l[i]<=r[j] then a[k]=l[i]; i+=1; k+=1
            else a[k]=r[j]; j+=1; k+=1 end
        end
        while i<n1; a[k]=l[i]; i+=1; k+=1; end
        while j<n2; a[k]=r[j]; j+=1; k+=1; end
    end
    right=a.size-1 if right==-1
    return if left>=right
    middle = (left+right-1)/2
    merge_sort(a, left, middle)
    merge_sort(a, middle+1, right)
    merge(a, left, middle, right)
end
arr = [9,1,22,4,0,-1,1,22,100,10]
merge_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

9 같이 보기

10 참고

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