Skip to content
Snippets Groups Projects
tris.c 1.65 KiB
Newer Older
Ukhanov Ilya's avatar
Ukhanov Ilya committed
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "tris.h"

Ukhanov Ilya's avatar
Ukhanov Ilya committed
void triInsertion(int* A, int n) {
malric litiere's avatar
malric litiere committed
    for(int i = 1; i <= n-1; i++){
        int cle = A[i];
        int j = i - 1;
        while(j >= 0 && A[j] > cle){
malric litiere's avatar
malric litiere committed
            A[j+1] = A[j];
Ukhanov Ilya's avatar
Ukhanov Ilya committed
            j--;
malric litiere's avatar
malric litiere committed
        }
        A[j+1] = cle;
malric litiere's avatar
malric litiere committed
    }
Ukhanov Ilya's avatar
Ukhanov Ilya committed
}
Ukhanov Ilya's avatar
Ukhanov Ilya committed

void triFusion(long* A, size_t size) {
  sousTriFusion(A, 0, size);
  printf("Tri fusion\n");
malric litiere's avatar
malric litiere committed
}

Ukhanov Ilya's avatar
Ukhanov Ilya committed
void sousTriFusion(long* A, size_t first, size_t size) {
    if(first<size-1){
        size_t middle = floor((first+size)/2);
        sousTriFusion(A, first, middle);
        sousTriFusion(A, middle, size);
        fusion(A, first, middle, size);
    }
    printf("Sous Tri fusion\n");
}

void copySousTable(long* mainT, long* underT, size_t id, size_t size) {
  size_t i = 0;
  for(; id < size; ++id) {
    mainT[id] = underT[i];
  }
}
malric litiere's avatar
malric litiere committed

Ukhanov Ilya's avatar
Ukhanov Ilya committed
void fusion(long* A, size_t first, size_t middle, size_t size) {
    size_t n1 = middle - first; // Nb elem dans A[p , q]  q exclu
    size_t n2 = size - middle;  // Nb elem dans A[q , r]  r exclu

    size_t indg = 0;
    size_t indd = 0;

    long* ag = malloc(sizeof(long) * n1);
    long* ad = malloc(sizeof(long) * n2);
    printf("Malloc\n");

    copySousTable(A, ag, first, n1);
    copySousTable(A, ad, middle, n2);
    printf("CopySousTable\n");

    int i = first;

    while(i < size) {
      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] = ag[indg];
        indd++;
      }
      ++i;
malric litiere's avatar
malric litiere committed
    }
Ukhanov Ilya's avatar
Ukhanov Ilya committed

    free(ag);
    free(ad);
}