Accueil > Forum > > > > Help :: Tri-Fusion itératif !!
Help :: Tri-Fusion itératif !!
jeudi 3 avril 2003 à 14:15:02 |
Help :: Tri-Fusion itératif !!

daarkon666
|
Salut !! Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! ^?^ Daarkon ^?^
|
|
jeudi 3 avril 2003 à 14:26:50 |
Re : Help :: Tri-Fusion itératif !!

BruNews
|
Voila le quicksort issu de la CRT, aucune recursion.
void SmallSort(int *lo, int *hi) { int *p, *max; int tmp; while(hi > lo) { max = lo; for(p = lo + 1; p <= hi; p++) { if(*p > *max) max = p; } tmp = *max; *max = *hi; *hi = tmp; hi--; } }
void QuickSort(int *base, unsigned num) { int *lo, *hi; /* ends of sub-array currently sorting */ int *mid; /* points to middle of subarray */ int *loguy, *higuy; /* traveling pointers for partition step */ unsigned size; /* size of the sub-array */ int *lostk[30], *histk[30]; int stkptr; /* stack for saving sub-array to be processed */ int tmp; /* Note: the number of stack entries required is no more than 1 + log2(size), so 30 is sufficient for any array */ if(num < 2) return; /* nothing to do */ stkptr = 0; /* initialize stack */ lo = base; hi = base + num - 1; recurse: size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ /* below a certain size, it is faster to use a O(n^2) sorting method */ if(size <= 8) SmallSort(lo, hi); else { mid = lo + (size / 2); /* find middle element */ tmp = *mid; *mid = *lo; *lo = tmp; loguy = lo; higuy = hi + 1; /* higuy decreases and loguy increases on every iteration, so loop must terminate. */ for(;;) { do { loguy++; } while(loguy <= hi && (*loguy <= *lo)); do { higuy--; } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); if(higuy < loguy) break; tmp = *loguy; *loguy = *higuy; *higuy = tmp; } tmp = *lo; *lo = *higuy; *higuy = tmp; if(higuy - 1 - lo >= hi - loguy) { if(lo + 1 < higuy) { lostk[stkptr] = lo; histk[stkptr] = higuy - 1; ++stkptr; /* save big recursion for later */ } if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ } else { if(loguy < hi) { lostk[stkptr] = loguy; histk[stkptr] = hi; ++stkptr; /* save big recursion for later */ } if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ } } --stkptr; if(stkptr >= 0) { lo = lostk[stkptr]; hi = histk[stkptr]; goto recurse; /* pop subarray from stack */ } }
BruNews, ciao...
------------------------------- Réponse au message : -------------------------------
> Salut !! > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > ^?^ Daarkon ^?^
|
|
jeudi 3 avril 2003 à 16:02:39 |
Re : Help :: Tri-Fusion itératif !!

daarkon666
|
hmmm oui ... why not, mais il faut une fonction qui soit linéaire (impératif, pour conserver la complexité Nlog(N) du tri-fusion) et qui ne ne teste qu'une fois chaque valeur !! et ce, sans aucune récursivité ni aucun "goto" ... lol
^?^ Daarkon ^?^
------------------------------- Réponse au message : -------------------------------
> Voila le quicksort issu de la CRT, aucune recursion. > > void SmallSort(int *lo, int *hi) > { > int *p, *max; > int tmp; > while(hi > lo) { > max = lo; > for(p = lo + 1; p <= hi; p++) { > if(*p > *max) max = p; > } > tmp = *max; *max = *hi; *hi = tmp; > hi--; > } > } > > void QuickSort(int *base, unsigned num) > { > int *lo, *hi; /* ends of sub-array currently sorting */ > int *mid; /* points to middle of subarray */ > int *loguy, *higuy; /* traveling pointers for partition step */ > unsigned size; /* size of the sub-array */ > int *lostk[30], *histk[30]; > int stkptr; /* stack for saving sub-array to be processed */ > int tmp; > /* Note: the number of stack entries required is no more than > 1 + log2(size), so 30 is sufficient for any array */ > if(num < 2) return; /* nothing to do */ > stkptr = 0; /* initialize stack */ > lo = base; hi = base + num - 1; > recurse: > size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ > /* below a certain size, it is faster to use a O(n^2) sorting method */ > if(size <= 8) SmallSort(lo, hi); > else { > mid = lo + (size / 2); /* find middle element */ > tmp = *mid; *mid = *lo; *lo = tmp; > loguy = lo; higuy = hi + 1; > /* higuy decreases and loguy increases on every iteration, > so loop must terminate. */ > for(;;) { > do { > loguy++; > } while(loguy <= hi && (*loguy <= *lo)); > do { > higuy--; > } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); > if(higuy < loguy) break; > tmp = *loguy; *loguy = *higuy; *higuy = tmp; > } > tmp = *lo; *lo = *higuy; *higuy = tmp; > if(higuy - 1 - lo >= hi - loguy) { > if(lo + 1 < higuy) { > lostk[stkptr] = lo; histk[stkptr] = higuy - 1; > ++stkptr; /* save big recursion for later */ > } > if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ > } > else { > if(loguy < hi) { > lostk[stkptr] = loguy; histk[stkptr] = hi; > ++stkptr; /* save big recursion for later */ > } > if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ > } > } > --stkptr; > if(stkptr >= 0) { > lo = lostk[stkptr]; > hi = histk[stkptr]; > goto recurse; /* pop subarray from stack */ > } > } > > BruNews, ciao... > > > ------------------------------- > Réponse au message : > ------------------------------- > > > Salut !! > > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > > > ^?^ Daarkon ^?^ >
|
|
jeudi 3 avril 2003 à 16:14:33 |
Re : Help :: Tri-Fusion itératif !!

BruNews
|
Tu as vu une recursivite dans ce code ? Pour goto suffit de pas le regarder, ton compilo en mettra en place des if case etc... Tu dvrais faire tests avec et comparer les temps. La meme traitee toute en interne. void QuickSort(int *base, unsigned num) { int *lo, *hi; /* ends of sub-array currently sorting */ int *mid; /* points to middle of subarray */ int *loguy, *higuy; /* traveling pointers for partition step */ unsigned size; /* size of the sub-array */ int *lostk[30], *histk[30]; int stkptr; /* stack for saving sub-array to be processed */ int tmp; /* Note: the number of stack entries required is no more than 1 + log2(size), so 30 is sufficient for any array */ if(num < 2) return; /* nothing to do */ stkptr = 0; /* initialize stack */ lo = base; hi = base + num - 1; recurse: size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ /* below a certain size, it is faster to use a O(n^2) sorting method */ if(size <= 8) { while(hi > lo) { mid = lo; for(loguy = lo + 1; loguy <= hi; loguy++) if(*loguy > *mid) mid = loguy; tmp = *mid; *mid = *hi; *hi = tmp; hi--; } goto fromSmall; } mid = lo + (size / 2); /* find middle element */ tmp = *mid; *mid = *lo; *lo = tmp; loguy = lo; higuy = hi + 1; /* higuy decreases and loguy increases on every iteration, so loop must terminate. */ for(;;) { do { loguy++; } while(loguy <= hi && (*loguy <= *lo)); do { higuy--; } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); if(higuy < loguy) break; tmp = *loguy; *loguy = *higuy; *higuy = tmp; } tmp = *lo; *lo = *higuy; *higuy = tmp; if(higuy - 1 - lo >= hi - loguy) { if(lo + 1 < higuy) { lostk[stkptr] = lo; histk[stkptr] = higuy - 1; ++stkptr; /* save big recursion for later */ } if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ } else { if(loguy < hi) { lostk[stkptr] = loguy; histk[stkptr] = hi; ++stkptr; /* save big recursion for later */ } if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ } fromSmall: --stkptr; if(stkptr >= 0) { lo = lostk[stkptr]; hi = histk[stkptr]; goto recurse; /* pop subarray from stack */ } }
BruNews, ciao...
------------------------------- Réponse au message : -------------------------------
> hmmm oui ... why not, mais il faut une fonction qui soit linéaire (impératif, pour conserver la complexité Nlog(N) du tri-fusion) et qui ne ne teste qu'une fois chaque valeur !! et ce, sans aucune récursivité ni aucun "goto" ... lol > > ^?^ Daarkon ^?^ > > > ------------------------------- > Réponse au message : > ------------------------------- > > > Voila le quicksort issu de la CRT, aucune recursion. > > > > void SmallSort(int *lo, int *hi) > > { > > int *p, *max; > > int tmp; > > while(hi > lo) { > > max = lo; > > for(p = lo + 1; p <= hi; p++) { > > if(*p > *max) max = p; > > } > > tmp = *max; *max = *hi; *hi = tmp; > > hi--; > > } > > } > > > > void QuickSort(int *base, unsigned num) > > { > > int *lo, *hi; /* ends of sub-array currently sorting */ > > int *mid; /* points to middle of subarray */ > > int *loguy, *higuy; /* traveling pointers for partition step */ > > unsigned size; /* size of the sub-array */ > > int *lostk[30], *histk[30]; > > int stkptr; /* stack for saving sub-array to be processed */ > > int tmp; > > /* Note: the number of stack entries required is no more than > > 1 + log2(size), so 30 is sufficient for any array */ > > if(num < 2) return; /* nothing to do */ > > stkptr = 0; /* initialize stack */ > > lo = base; hi = base + num - 1; > > recurse: > > size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ > > /* below a certain size, it is faster to use a O(n^2) sorting method */ > > if(size <= 8) SmallSort(lo, hi); > > else { > > mid = lo + (size / 2); /* find middle element */ > > tmp = *mid; *mid = *lo; *lo = tmp; > > loguy = lo; higuy = hi + 1; > > /* higuy decreases and loguy increases on every iteration, > > so loop must terminate. */ > > for(;;) { > > do { > > loguy++; > > } while(loguy <= hi && (*loguy <= *lo)); > > do { > > higuy--; > > } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); > > if(higuy < loguy) break; > > tmp = *loguy; *loguy = *higuy; *higuy = tmp; > > } > > tmp = *lo; *lo = *higuy; *higuy = tmp; > > if(higuy - 1 - lo >= hi - loguy) { > > if(lo + 1 < higuy) { > > lostk[stkptr] = lo; histk[stkptr] = higuy - 1; > > ++stkptr; /* save big recursion for later */ > > } > > if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ > > } > > else { > > if(loguy < hi) { > > lostk[stkptr] = loguy; histk[stkptr] = hi; > > ++stkptr; /* save big recursion for later */ > > } > > if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ > > } > > } > > --stkptr; > > if(stkptr >= 0) { > > lo = lostk[stkptr]; > > hi = histk[stkptr]; > > goto recurse; /* pop subarray from stack */ > > } > > } > > > > BruNews, ciao... > > > > > > ------------------------------- > > Réponse au message : > > ------------------------------- > > > > > Salut !! > > > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > > > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > > > > > ^?^ Daarkon ^?^ > > >
|
|
jeudi 3 avril 2003 à 16:37:52 |
Re : Help :: Tri-Fusion itératif !!

daarkon666
|
y a pas de récursivité c'est vrai ... mais meme si le compilateur remplacera les goto par autre chose, qd mon prof verra le source il gueulera pcq on n'a jamais utilisé aucun goto ;-) en fait ... voilà comment je pars pr le tri/fusion itératif : je considère mon tableau comme une suite de "boites" de 1 élément, donc ces boites contiennent bien des éléments triés (un élément seul est toujours trié !!) ... suite à quoi j'augmente par 2 la taille des listes, pour trier/fusionner des listes de 2 éléments et ainsi de suite, en prenant en compte la possibilité que la taille des listes soit > taille du tableau, auquel cas je rectifie dans ma fonction "fusion" ... mais le pb est que soit cette fonction ne s'arrete pas (alors qu'il y a bien un critère d'arret), soit elle me remplace des éléments du tableau de base par des 0 !!! voici la version actuelle de Fusionner (note : t2 est un tableau créé dans la fonction tri/fusion en temporaire, et t est le tableau initial a trier) :
// trie et fusionne en une passe les éléments de 2 sous-tableaux
void fusionner(int *t, int *t2 , int deb, int longueur, const long int tailleTab) { // [deb ; deb+longueur] = [i ; iMax] U [j ; jMax] int i ; int iMax = deb + longueur/2 - 1 ; int j = deb + longueur/2 ; // ou j = iMax+1 int jMax = deb + longueur ; int fait = 0 ;
// cas extremes si on dépasse la taille du tableau if ( iMax >= tailleTab ) iMax = tailleTab-1 ;
if ( jMax >= tailleTab ) jMax = tailleTab-1 ;
// recopie du tableau initial ds le tableau temporaire for(i=0 ; i<longueur ; i++) { t2 = t[i] ; }
i = deb ;
while( fait == 0 ) { if(i<=iMax && j<=jMax) { if( t2[i]<t2[j] ) // t2[i] < t2[j] ? si oui t2[i] -> t[i] { t[i] = t2[i] ; i=i++ ; } else // sinon t2[j] -> t[i] ; { t[i] = t2[j] ; i=i++ ; j=j++ ; } } else { if(i<=iMax) { t[i] = t2[i] ; i++ ; } else if (j<=jMax) { t[i] = t2[j] ; i++ ; j++ ; } else fait == 1 ; } } } where is the bug ??
^?^ Daarkon ^?^
------------------------------- Réponse au message : -------------------------------
> Tu as vu une recursivite dans ce code ? > Pour goto suffit de pas le regarder, ton compilo en mettra en place des if case etc... > Tu dvrais faire tests avec et comparer les temps. > La meme traitee toute en interne. > void QuickSort(int *base, unsigned num) > { > int *lo, *hi; /* ends of sub-array currently sorting */ > int *mid; /* points to middle of subarray */ > int *loguy, *higuy; /* traveling pointers for partition step */ > unsigned size; /* size of the sub-array */ > int *lostk[30], *histk[30]; > int stkptr; /* stack for saving sub-array to be processed */ > int tmp; > /* Note: the number of stack entries required is no more than > 1 + log2(size), so 30 is sufficient for any array */ > if(num < 2) return; /* nothing to do */ > stkptr = 0; /* initialize stack */ > lo = base; hi = base + num - 1; > recurse: > size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ > /* below a certain size, it is faster to use a O(n^2) sorting method */ > if(size <= 8) { > while(hi > lo) { > mid = lo; > for(loguy = lo + 1; loguy <= hi; loguy++) if(*loguy > *mid) mid = loguy; > tmp = *mid; *mid = *hi; *hi = tmp; > hi--; > } > goto fromSmall; > } > mid = lo + (size / 2); /* find middle element */ > tmp = *mid; *mid = *lo; *lo = tmp; > loguy = lo; higuy = hi + 1; > /* higuy decreases and loguy increases on every iteration, > so loop must terminate. */ > for(;;) { > do { > loguy++; > } while(loguy <= hi && (*loguy <= *lo)); > do { > higuy--; > } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); > if(higuy < loguy) break; > tmp = *loguy; *loguy = *higuy; *higuy = tmp; > } > tmp = *lo; *lo = *higuy; *higuy = tmp; > if(higuy - 1 - lo >= hi - loguy) { > if(lo + 1 < higuy) { > lostk[stkptr] = lo; histk[stkptr] = higuy - 1; > ++stkptr; /* save big recursion for later */ > } > if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ > } > else { > if(loguy < hi) { > lostk[stkptr] = loguy; histk[stkptr] = hi; > ++stkptr; /* save big recursion for later */ > } > if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ > } > fromSmall: > --stkptr; > if(stkptr >= 0) { > lo = lostk[stkptr]; > hi = histk[stkptr]; > goto recurse; /* pop subarray from stack */ > } > } > > BruNews, ciao... > > > ------------------------------- > Réponse au message : > ------------------------------- > > > hmmm oui ... why not, mais il faut une fonction qui soit linéaire (impératif, pour conserver la complexité Nlog(N) du tri-fusion) et qui ne ne teste qu'une fois chaque valeur !! et ce, sans aucune récursivité ni aucun "goto" ... lol > > > > ^?^ Daarkon ^?^ > > > > > > ------------------------------- > > Réponse au message : > > ------------------------------- > > > > > Voila le quicksort issu de la CRT, aucune recursion. > > > > > > void SmallSort(int *lo, int *hi) > > > { > > > int *p, *max; > > > int tmp; > > > while(hi > lo) { > > > max = lo; > > > for(p = lo + 1; p <= hi; p++) { > > > if(*p > *max) max = p; > > > } > > > tmp = *max; *max = *hi; *hi = tmp; > > > hi--; > > > } > > > } > > > > > > void QuickSort(int *base, unsigned num) > > > { > > > int *lo, *hi; /* ends of sub-array currently sorting */ > > > int *mid; /* points to middle of subarray */ > > > int *loguy, *higuy; /* traveling pointers for partition step */ > > > unsigned size; /* size of the sub-array */ > > > int *lostk[30], *histk[30]; > > > int stkptr; /* stack for saving sub-array to be processed */ > > > int tmp; > > > /* Note: the number of stack entries required is no more than > > > 1 + log2(size), so 30 is sufficient for any array */ > > > if(num < 2) return; /* nothing to do */ > > > stkptr = 0; /* initialize stack */ > > > lo = base; hi = base + num - 1; > > > recurse: > > > size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ > > > /* below a certain size, it is faster to use a O(n^2) sorting method */ > > > if(size <= 8) SmallSort(lo, hi); > > > else { > > > mid = lo + (size / 2); /* find middle element */ > > > tmp = *mid; *mid = *lo; *lo = tmp; > > > loguy = lo; higuy = hi + 1; > > > /* higuy decreases and loguy increases on every iteration, > > > so loop must terminate. */ > > > for(;;) { > > > do { > > > loguy++; > > > } while(loguy <= hi && (*loguy <= *lo)); > > > do { > > > higuy--; > > > } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); > > > if(higuy < loguy) break; > > > tmp = *loguy; *loguy = *higuy; *higuy = tmp; > > > } > > > tmp = *lo; *lo = *higuy; *higuy = tmp; > > > if(higuy - 1 - lo >= hi - loguy) { > > > if(lo + 1 < higuy) { > > > lostk[stkptr] = lo; histk[stkptr] = higuy - 1; > > > ++stkptr; /* save big recursion for later */ > > > } > > > if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ > > > } > > > else { > > > if(loguy < hi) { > > > lostk[stkptr] = loguy; histk[stkptr] = hi; > > > ++stkptr; /* save big recursion for later */ > > > } > > > if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ > > > } > > > } > > > --stkptr; > > > if(stkptr >= 0) { > > > lo = lostk[stkptr]; > > > hi = histk[stkptr]; > > > goto recurse; /* pop subarray from stack */ > > > } > > > } > > > > > > BruNews, ciao... > > > > > > > > > ------------------------------- > > > Réponse au message : > > > ------------------------------- > > > > > > > Salut !! > > > > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > > > > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > > > > > > > [i]^?^ Daarkon ^?^ > > > > > >
|
|
jeudi 3 avril 2003 à 17:26:28 |
Re : Help :: Tri-Fusion itératif !!

BruNews
|
Tu m'aurais dit de suite que c'est pour l'ecole qui ne veut pas de goto, je n'insistais pas. Pas le temps d'aller + loin pour l'instant. BruNews, ciao...
------------------------------- Réponse au message : -------------------------------
> y a pas de récursivité c'est vrai ... mais meme si le compilateur remplacera les goto par autre chose, qd mon prof verra le source il gueulera pcq on n'a jamais utilisé aucun goto ;-) > en fait ... voilà comment je pars pr le tri/fusion itératif : > je considère mon tableau comme une suite de "boites" de 1 élément, donc ces boites contiennent bien des éléments triés (un élément seul est toujours trié !!) ... suite à quoi j'augmente par 2 la taille des listes, pour trier/fusionner des listes de 2 éléments et ainsi de suite, en prenant en compte la possibilité que la taille des listes soit > taille du tableau, auquel cas je rectifie dans ma fonction "fusion" ... mais le pb est que soit cette fonction ne s'arrete pas (alors qu'il y a bien un critère d'arret), soit elle me remplace des éléments du tableau de base par des 0 !!! > voici la version actuelle de Fusionner (note : t2 est un tableau créé dans la fonction tri/fusion en temporaire, et t est le tableau initial a trier) : > > // trie et fusionne en une passe les éléments de 2 sous-tableaux > > void fusionner(int *t, int *t2 , int deb, int longueur, const long int tailleTab) > { > // [deb ; deb+longueur] = [i ; iMax] U [j ; jMax] > int i ; > int iMax = deb + longueur/2 - 1 ; > int j = deb + longueur/2 ; // ou j = iMax+1 > int jMax = deb + longueur ; > int fait = 0 ; > > // cas extremes si on dépasse la taille du tableau > if ( iMax >= tailleTab ) > iMax = tailleTab-1 ; > > if ( jMax >= tailleTab ) > jMax = tailleTab-1 ; > > // recopie du tableau initial ds le tableau temporaire > for(i=0 ; i<longueur ; i++) > { > t2 = t[i] ; > } > > i = deb ; > > while( fait == 0 ) > { > if(i<=iMax && j<=jMax) > { > if( t2[i]<t2[j] ) // t2[i] < t2[j] ? si oui t2[i] -> t[i] > { > t[i] = t2[i] ; > i=i++ ; > } > else // sinon t2[j] -> t[i] ; > { > t[i] = t2[j] ; > i=i++ ; > j=j++ ; > } > } > else > { > if(i<=iMax) > { > t[i] = t2[i] ; > i++ ; > } > else if (j<=jMax) > { > t[i] = t2[j] ; > i++ ; > j++ ; > } > else > fait == 1 ; > } > } > } > > where is the bug ?? > > ^?^ Daarkon ^?^ > > > ------------------------------- > Réponse au message : > ------------------------------- > > > Tu as vu une recursivite dans ce code ? > > Pour goto suffit de pas le regarder, ton compilo en mettra en place des if case etc... > > Tu dvrais faire tests avec et comparer les temps. > > La meme traitee toute en interne. > > void QuickSort(int *base, unsigned num) > > { > > int *lo, *hi; /* ends of sub-array currently sorting */ > > int *mid; /* points to middle of subarray */ > > int *loguy, *higuy; /* traveling pointers for partition step */ > > unsigned size; /* size of the sub-array */ > > int *lostk[30], *histk[30]; > > int stkptr; /* stack for saving sub-array to be processed */ > > int tmp; > > /* Note: the number of stack entries required is no more than > > 1 + log2(size), so 30 is sufficient for any array */ > > if(num < 2) return; /* nothing to do */ > > stkptr = 0; /* initialize stack */ > > lo = base; hi = base + num - 1; > > recurse: > > size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ > > /* below a certain size, it is faster to use a O(n^2) sorting method */ > > if(size <= 8) { > > while(hi > lo) { > > mid = lo; > > for(loguy = lo + 1; loguy <= hi; loguy++) if(*loguy > *mid) mid = loguy; > > tmp = *mid; *mid = *hi; *hi = tmp; > > hi--; > > } > > goto fromSmall; > > } > > mid = lo + (size / 2); /* find middle element */ > > tmp = *mid; *mid = *lo; *lo = tmp; > > loguy = lo; higuy = hi + 1; > > /* higuy decreases and loguy increases on every iteration, > > so loop must terminate. */ > > for(;;) { > > do { > > loguy++; > > } while(loguy <= hi && (*loguy <= *lo)); > > do { > > higuy--; > > } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); > > if(higuy < loguy) break; > > tmp = *loguy; *loguy = *higuy; *higuy = tmp; > > } > > tmp = *lo; *lo = *higuy; *higuy = tmp; > > if(higuy - 1 - lo >= hi - loguy) { > > if(lo + 1 < higuy) { > > lostk[stkptr] = lo; histk[stkptr] = higuy - 1; > > ++stkptr; /* save big recursion for later */ > > } > > if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ > > } > > else { > > if(loguy < hi) { > > lostk[stkptr] = loguy; histk[stkptr] = hi; > > ++stkptr; /* save big recursion for later */ > > } > > if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ > > } > > fromSmall: > > --stkptr; > > if(stkptr >= 0) { > > lo = lostk[stkptr]; > > hi = histk[stkptr]; > > goto recurse; /* pop subarray from stack */ > > } > > } > > > > BruNews, ciao... > > > > > > ------------------------------- > > Réponse au message : > > ------------------------------- > > > > > hmmm oui ... why not, mais il faut une fonction qui soit linéaire (impératif, pour conserver la complexité Nlog(N) du tri-fusion) et qui ne ne teste qu'une fois chaque valeur !! et ce, sans aucune récursivité ni aucun "goto" ... lol > > > > > > ^?^ Daarkon ^?^ > > > > > > > > > ------------------------------- > > > Réponse au message : > > > ------------------------------- > > > > > > > Voila le quicksort issu de la CRT, aucune recursion. > > > > > > > > void SmallSort(int *lo, int *hi) > > > > { > > > > int *p, *max; > > > > int tmp; > > > > while(hi > lo) { > > > > max = lo; > > > > for(p = lo + 1; p <= hi; p++) { > > > > if(*p > *max) max = p; > > > > } > > > > tmp = *max; *max = *hi; *hi = tmp; > > > > hi--; > > > > } > > > > } > > > > > > > > void QuickSort(int *base, unsigned num) > > > > { > > > > int *lo, *hi; /* ends of sub-array currently sorting */ > > > > int *mid; /* points to middle of subarray */ > > > > int *loguy, *higuy; /* traveling pointers for partition step */ > > > > unsigned size; /* size of the sub-array */ > > > > int *lostk[30], *histk[30]; > > > > int stkptr; /* stack for saving sub-array to be processed */ > > > > int tmp; > > > > /* Note: the number of stack entries required is no more than > > > > 1 + log2(size), so 30 is sufficient for any array */ > > > > if(num < 2) return; /* nothing to do */ > > > > stkptr = 0; /* initialize stack */ > > > > lo = base; hi = base + num - 1; > > > > recurse: > > > > size = (hi - lo) / sizeof(int) + 1; /* number of el's to sort */ > > > > /* below a certain size, it is faster to use a O(n^2) sorting method */ > > > > if(size <= 8) SmallSort(lo, hi); > > > > else { > > > > mid = lo + (size / 2); /* find middle element */ > > > > tmp = *mid; *mid = *lo; *lo = tmp; > > > > loguy = lo; higuy = hi + 1; > > > > /* higuy decreases and loguy increases on every iteration, > > > > so loop must terminate. */ > > > > for(;;) { > > > > do { > > > > loguy++; > > > > } while(loguy <= hi && (*loguy <= *lo)); > > > > do { > > > > higuy--; > > > > } while(higuy > lo && (*(int*)higuy >= *(int*)lo)); > > > > if(higuy < loguy) break; > > > > tmp = *loguy; *loguy = *higuy; *higuy = tmp; > > > > } > > > > tmp = *lo; *lo = *higuy; *higuy = tmp; > > > > if(higuy - 1 - lo >= hi - loguy) { > > > > if(lo + 1 < higuy) { > > > > lostk[stkptr] = lo; histk[stkptr] = higuy - 1; > > > > ++stkptr; /* save big recursion for later */ > > > > } > > > > if(loguy < hi) {lo = loguy; goto recurse;} /* do small recursion */ > > > > } > > > > else { > > > > if(loguy < hi) { > > > > lostk[stkptr] = loguy; histk[stkptr] = hi; > > > > ++stkptr; /* save big recursion for later */ > > > > } > > > > if(lo + 1 < higuy) {hi = higuy - 1; goto recurse;} /* do small recursion */ > > > > } > > > > } > > > > --stkptr; > > > > if(stkptr >= 0) { > > > > lo = lostk[stkptr]; > > > > hi = histk[stkptr]; > > > > goto recurse; /* pop subarray from stack */ > > > > } > > > > } > > > > > > > > BruNews, ciao... > > > > > > > > > > > > ------------------------------- > > > > Réponse au message : > > > > ------------------------------- > > > > > > > > > Salut !! > > > > > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > > > > > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > > > > > > > > > [i]^?^ Daarkon ^?^ > > > > > > > > > >
|
|
jeudi 1 mai 2003 à 13:50:37 |
Re : Help :: Tri-Fusion itératif !!

SuperMonkey
|
T'as que regarder sur celui de Smimit ;) J'éspére que t'as réussi sinon, m'sieur négre y vas pô aimer ;)
A+ Damz
------------------------------- Réponse au message : -------------------------------
> Salut !! > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > ^?^ Daarkon ^?^
|
|
jeudi 1 mai 2003 à 18:13:47 |
Re : Help :: Tri-Fusion itératif !!

daarkon666
|
Hoohooooo Damieeen !! T'es dingue, je vais pas prendre un algo de qqn d'autre, et encore moins celui d'un nul comme Smimite !! mdrr Nan mais c'est bon, g trouvé depuis pas mal de temps déjà (un dessin sur papier a suffi !!) ;-) Meme le rapport est prêt ... hihi
^¤^ Daarkon ^¤^
------------------------------- Réponse au message : -------------------------------
> T'as que regarder sur celui de Smimit ;) > J'éspére que t'as réussi sinon, m'sieur négre y vas pô aimer ;) > > A+ Damz > > > > ------------------------------- > Réponse au message : > ------------------------------- > > > Salut !! > > Je planche actuellement sur une version itérative du Tri-Fusion, et y a un pb : je ne vois pas comment écrire la fonction fusion qui se charge de trier/fusionner deux listes d'éléments non-triés !! > > Qqn aurait un algo (en C) ou une piste à me lancer svp ?? Merci d'avance !! > > > > ^?^ Daarkon ^?^
|
|
Cette discussion est classée dans : help, font, tri, fusion, itératif
Répondre à ce message
Sujets en rapport avec ce message
help : raffraichir l ecran? [ par g2fx ]
bonjour , je code pour l instant de petit prog afin de me familiariser avec le c , et je cherche la commande equivalente a clrscr(); pour visual c++6.
please help me dx8 [ par bibou75 ]
cherche toute personne pouvant me renseigner comment faire pour afficher un buffer pixel qui provient d'une webcam ou d'une carte d'acquisitionje vous
Complexité de l'algorithme de Tri Fusion [ par ousin ]
Salut tout le monde, je voudrais de l'aide pour demontrer mathematiquement en urilisant la resolution des reccurences que la complexité du Tri Fusion
Pb de tri et taille de tableaux [ par daarkon666 ]
Salut !!Je viens de terminer le tri/fusion itératif (et d'autres algos de tri, pr un projet info de fac) en C, et je suis soumis à un pb auss ibien so
TRI FUSION avec des Threads [ par ariabbas ]
BONJOUR, j'ai un problème et je recherche une solution . Ecrire un programmae implementant une VERSION DU TRI-FUSION à l'aide des THREAD en C. Le
algorithme de tri hoare [ par alinformatik ]
au cours des travaux pratiques en module de système d'exploitation, pour comprendre la synchronisation des processus sous linux on nous a demandé d'éc
Création et gestion de Base de Données en C++. Help [ par Iziwschi ]
Bonsoir tout le monde. Je débute en C++, j'aurai donc besoin de votre aide. En effet je souhaite créer une base de données avec une ou deux tables, qu
tri diagonale d'une matrice [ par realy23 ]
[img][URL=http://www.hostingpics.net/viewer.php?id=160888drap.png][IMG]http://img11.hostingpics.net/pics/160888drap.png[/IMG][/URL][/img] svp veuill
Livres en rapport
|
Derniers Blogs
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Forum
ALGORITHMESALGORITHMES par whayoub
Cliquez pour lire la suite par whayoub
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|