void triFusion(int * tab, int n ){ int pipe1[2]; int pipe2[2]; if(n<2){ write(stdout,tab,n); } else{ pipeP[2] pipe(pipe[0]); pipe(pipe[1]); pid_t p1 = fork(); if(p1 == 0){ dup2(pipe1[1],stdout); triFusion(tab,n/2); } else{ int p2 = fork(); if(p2 == 0){ dup2(pipe[1],stdout); triFusion(tab+n/2,n/2); } else{ int tmp[2]; int pi = 0; read(pipe1[0],&tmp[0],BUFFSIZE); read(pipe2[1],&tmp[1],BUFFSIZE); if(tmp[0] > tmp[1]) { pi = 1; write(stdout,&tmp[1],1); } else{ pi=0 write(stdout,&tmp[0],1) } while(read(pipeP[pi][0],&tmp[pi],BUFFSIZE) != -1){ //read(pipe[pi],&tmp[pi],BUFFSIZE); if(tmp[0] > tmp[1]) { pi = 1; write(stdout,&tmp[1],1); } else{ pi=0 write(stdout,&tmp[0],1) } } } } } }
void main() { srand(time(NULL)); Liste l = vide(); Liste m = vide(); Liste n = vide(); int t, i; for(i = 0; i < 10; i++) { t = rand()%100; l = ajouttri(t, l); t = rand()%100; m = ajouttri(t, m); affiche(l); affiche(m); t = rand()%100; printf("\n %d, %d\n", t, src(l, t)); } triBulle(l); affiche(l); l = flip(l, vide()); affiche(l); l = triFusion(l); affiche(l); t = testtri(l); printf("\ntri = %d \n", t); printf("\ntaille = %d \n", taille(l)); }
Liste triFusion(Liste l) { int i; Liste res = vide(); Liste tmp1 = l; Liste tmp2; if(l->nxt == NULL) { return(l); } for(i = 0; i < taille(l)/2 - 1; i++) { tmp1 = tmp1->nxt; } tmp2 = tmp1->nxt; tmp1->nxt = NULL; affiche(l); affiche(tmp2); return(concattri(triFusion(l),triFusion(tmp2), res)); }