Harigami
ログイン
anonymous タイトルなし
No License C
コピー
#include<stdio.h>
#define num 10

void mergesort(int ary[], int left, int right){

    int i, j, k, mid;
    int temp[num];

    if(left < right){

        mid = (left + right)/2;

        mergesort(ary, left, mid);
        mergesort(ary, mid + 1, right);

        for(i = left; i <= mid; i++){
            temp[i] = ary[i];
        }

        for(j = mid + 1; j <= right; j++){
            temp[right - (j - (mid + 1))] = ary[j];
        }

        i = left, j = right;

        for(k = left; k <= right; k++){
            if(temp[i] < temp[j]){
                ary[k] = temp[i];
                i++;
            }
            else{
                ary[k] = temp[j];
                j--;
            }
        }
    }
}

void merge(int ary1[], int ary2[], int ary3[]){
    int i, j, k;
    i = j = k = 0;

    for(; k < num * 2; k++){
        if(ary1[i] < ary2[j]){
            ary3[k] = ary1[i];
            i++;
        }
        else{
            ary3[k] = ary2[j];
            j++;
        }
    }
}

int main(void){
    int ary1[num], ary2[num], ary3[num * 2];

    for(int i = 0; i < num; i++){
        scanf("%d",&ary1[i]);
    }

    for(int i = 0; i < num; i++){
        scanf("%d",&ary2[i]);
    }

    mergesort(ary1,0,num - 1);

    mergesort(ary2,0,num - 1);

    for(int i = 0; i < num; i++){
        printf("%d,", ary1[i]);
    }
    printf("\n");

    for(int i = 0; i < num; i++){
        printf("%d,", ary2[i]);
    }
    printf("\n");

    merge(ary1, ary2, ary3);

    for(int i = 0; i < num * 2; i++){
        printf("%d, ",ary3[i]);
    }

    printf("\n");
}
#include<stdio.h>
#define num 10

void mergesort(int ary[], int left, int right){

    int i, j, k, mid;
    int temp[num];

    if(left < right){

        mid = (left + right)/2;

        mergesort(ary, left, mid);
        mergesort(ary, mid + 1, right);

        for(i = left; i <= mid; i++){
            temp[i] = ary[i];
        }

        for(j = mid + 1; j <= right; j++){
            temp[right - (j - (mid + 1))] = ary[j];
        }

        i = left, j = right;

        for(k = left; k <= right; k++){
            if(temp[i] < temp[j]){
                ary[k] = temp[i];
                i++;
            }
            else{
                ary[k] = temp[j];
                j--;
            }
        }
    }
}

void merge(int ary1[], int ary2[], int ary3[]){
    int i, j, k;
    i = j = k = 0;

    for(; k < num * 2; k++){
        if(ary1[i] < ary2[j]){
            ary3[k] = ary1[i];
            i++;
        }
        else{
            ary3[k] = ary2[j];
            j++;
        }
    }
}

int main(void){
    int ary1[num], ary2[num], ary3[num * 2];

    for(int i = 0; i < num; i++){
        scanf("%d",&ary1[i]);
    }

    for(int i = 0; i < num; i++){
        scanf("%d",&ary2[i]);
    }

    mergesort(ary1,0,num - 1);

    mergesort(ary2,0,num - 1);

    for(int i = 0; i < num; i++){
        printf("%d,", ary1[i]);
    }
    printf("\n");

    for(int i = 0; i < num; i++){
        printf("%d,", ary2[i]);
    }
    printf("\n");

    merge(ary1, ary2, ary3);

    for(int i = 0; i < num * 2; i++){
        printf("%d, ",ary3[i]);
    }

    printf("\n");
}
現在、コメントはありません。
最初のコメンターになりませんか?