Skip to content
Snippets Groups Projects
triFusion.c 1.06 KiB
Newer Older
Mickael Da Silva's avatar
Mickael Da Silva committed
#include "triFusion.h"

void sousTriFusion(long * A, size_t p, size_t r){
    if(p<(r-1)){
        size_t q = (size_t)((p+r)/2);
        sousTriFusion(A, p, q);
        sousTriFusion(A, q, r);
        fusion(A, p, q, r);
    }
}

void fusion(long * A, size_t p, size_t q, size_t r){
    size_t n1 = q-p;
    size_t n2 = r-q;

    long Ad[n2];
    memset(Ad, 0, n2);
    int j =0;
    for(size_t i = q; i<r; i++){
        Ad[j] = A[i];
        j++;
    }

    long Ag[n1];
    memset(Ag, 0, n1);
    j =0;
    for(size_t i = p; i<q; i++){
        Ag[j] = A[i];
        j++;
    }

    size_t indg = 0;
    size_t indd = 0;
    size_t i = p;
    while (i < r){
        if(indg == n1){
            A[i] = Ad[indd];
            indd++;
        }
        else if(indd == n2){
            A[i] == Ag[indg];
            indg++;
        }
        else if(Ag[indg] < Ad[indd]){
            A[i] = Ag[indg];
            indg++;
        }
        else{
            A[i] = Ad[indd];
            indd++;
        }
        i++;
    }
}

void triFusion(long * A, size_t n){
    sousTriFusion(A, 0, n);
}