Sunteți pe pagina 1din 30

Leon n.

16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

PROGRAMMATION AVANCEE
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Leon 16 : ALGORITHMES AVANCES DE TRI

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Plan de la leon
Tri par partition-fusion Tri rapide (quick sort) Tri par le tas (heap sort) Synthse

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par partitionfusion


Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par partition-fusion Description


On dispose dune squence S de n lments Le tri par partition-fusion suit le paradigme de diviser pour rsoudre Diviser : partitionner S en deux soussquences S1 et S2 de taille n/2 et trier rcursivement S1 et S2 Rsoudre : fusionner S1 et S2 en une unique squence trie
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par partition-fusion Illustration


77 22 99 44 33 88 66 11 1 2 3 4 6 7 8 9 7 2 9 43 8 6 1 1 2 3 4 6 7 8 9 1 2 3 4 6 7

7 2 9 4 2 4 7 9 7 2 99 44 2 4 7 9 7 29 4 2 4 7 9 2 4

3 8 6 1 1 3 8 6 3 8 6 1 1 3 6 8 3 8 6 1 1 3 8 6

72 2 7 7 22 2 7 722 7 7

9 4 4 9 9 4 4 9

3 8 3 8 3 8 3 8 3 8 3 8

6 1 1 6 6 1 1 6 6 1 1 6

77 77

Prof. Karim Bana


ENSIAS

22 22

99 99

44 44

33 33

88 88

66 66

11 11

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par partition-fusion Implantation


void Tri_par_partition_fusion (int * t, int d, int f){ if (f > d) { int m = (d + f) / 2; // partition Tri_par_partition_fusion(t, d, m); Tri_par_partition_fusion(t, m+1, f); Fusion(t, d, m, f); // fusion } }
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par partition-fusion Implantation


void Fusion (int * t, int d, int m, int f){ void Fusion (int * t, int d, int m, int f){ int * fus = (int*)malloc((f-d+1)*sizeof(int)); int * fus = (int*)malloc((f-d+1)*sizeof(int)); int ii = d, jj = m+1, k = 0; int = d, = m+1, k = 0; while ((i <= m) && (j <= f)) { while ((i <= m) && (j <= f)) { if (t[ ii ] <= t[ jj ]) { fus[ k++ ] = t[ i++ ]; } if (t[ ] <= t[ ]) { fus[ k++ ] = t[ i++ ]; } else { fus[ k++ ] = t[ j++ ]; } } else { fus[ k++ ] = t[ j++ ]; } } /* copie des lments restants */ /* copie des lments restants while (i <= m) { fus[ k++ ] = t[ i++ ]; } while (i <= m) { fus[ k++ ] = t[ i++ ]; } while (j <= f) { fus[ k++ ] = t[ j++ ]; } while (j <= f) { fus[ k++ ] = t[ j++ ]; } /* recopie du rsultat */ ii = d; k = 0; /* recopie du rsultat */ = d; k = 0; while (i <= f) { t[ i++ ] = fus[ k++ ]; } free(fus); } } while (i <= f) { t[ i++ ] = fus[ k++ ]; } free(fus); Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par partition-fusion Analyse de complexit


partition : calcul du milieu du tableau par addition et division en O(1) tri des deux squences de n/2 lment : en 2 *O(T(n/2)) fusion : parcours mutuels des deux squences en une seule fois en O(n)

T(1) = O(1), T(n) = 2 * T(n/2) + O(n) T(n) = O(n log n) (thorme matre)
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide (quick sort)


Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Description


On dispose dune squence S de n lments Le tri rapide est un tri alatoire qui suit le paradigme diviser pour rsoudre Diviser : choisir un lment x (pivot) parmi S et partitionner S en 3 sous-squences (1) I : lments infrieurs x, (2) E : lments gaux x et (3) S : lments suprieurs x. Trier I et S rcursivement Rsoudre : trier puis concatner I, E et S
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Description


(1) choix du pivot (2) partition
x I x
Prof. Karim Bana
ENSIAS

(3) tri de I et S

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Implantation


void Tri_rapide (int * t, int d, int f){
// invariant : d-f dcrot lors de la rcurrence // (dappel en appel) if ((d>=0) && (f>=0) && (d <= f){ int i, s; /* partitions I: t[d..i-1]-E: t[i..s-1]-S: t[s..f] */ Partition (t, d, f, &i, &s); Tri_rapide (t, d, i-1); // (i-1-d)<(f-d) (i-1)< f Tri_rapide (t, s, f); // (f-s)<(f-d) (d<s) } }

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Implantation


void Partition (int * t, int d, int f, int * i, int * s) { void Partition (int * t, int d, int f, int * i, int * s) int capacite = (f-d+1) * sizeof(int); int capacite = (f-d+1) * sizeof(int); int * I= (int *)malloc(capacite); int * I= (int *)malloc(capacite); int * S= (int*)malloc(capacite); int * S= (int*)malloc(capacite); /* slection du pivot */ /* slection du pivot */ int pivot = (rand() % (f-d+1)) +d; int pivot = (rand() % (f-d+1)) +d; // 0 <= rand() <= 32767 d <= pivot <= f // 0 <= rand() <= 32767 d <= pivot <= f int elementPivot = t[ pivot ]; int elementPivot = t[ pivot ];

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Implantation


void Partition (int * t, int d, int f, int * i, int * s) { void Partition (int * t, int d, int f, int * i, int * s)
. . int in = d, cardi = 0, carde = 0, cards = 0, out; int in = d, cardi = 0, carde = 0, cards = 0, out; while (in <= f){ /* Calcul de I et S */ while (in <= f){ /* Calcul de I et S */ if (t[ in ] < elementPivot) I[ cardi++ ] = t[in++]; if (t[ in ] < elementPivot) I[ cardi++ ] = t[in++]; else if (t[ in ] == elementPivot) { carde++; in++; } else if (t[ in ] == elementPivot) { carde++; in++; } else S[ cards++ ] = t[in++]; } else S[ cards++ ] = t[in++]; } *i = d+cardi; // [0+(d)<=cardi+(d)=(*i) <= f-d+(d)=f ] *i - 1<f *i = d+cardi; // [0+(d)<=cardi+(d)=(*i) <= f-d+(d)=f ] *i - 1<f *s = d+cardi+carde; // [1<(carde)<=cardi+(carde)=(*s)-d ] d<*s *s = d+cardi+carde; // [1<(carde)<=cardi+(carde)=(*s)-d ] d<*s in = 0; out = d; while (in < cardi) t[ out++ ] = I[ in++ ]; in = 0; out = d; while (in < cardi) t[ out++ ] = I[ in++ ]; in = 0; while (in < carde) { t[ out++ ] = elementPivot; in++;} in = 0; while (in < carde) { t[ out++ ] = elementPivot; in++;} in = 0; while (in < cards) t[ out++ ] = S[ in++ ]; in = 0; while (in < cards) t[ out++ ] = S[ in++ ]; free(S); free(I); } free(S); free(I); }

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Illustration


77 22 99 4433 77 66 11 1 12 23 34 46 67 78 89 9 3 7 6 1 77 22 99 44 3 7 6 1 1 2 3 4 6 7 8 9 7 7 9

22 44 33 11 1 2 4 7 7 2 9 4 2 3 9 2 4 7 4

7 9 7 1 3 78 96 71 17 37 89 6

21 21

9 4 4 9 4 3 3 4

88 3 3

9 88 9

Prof. Karim Bana


ENSIAS

99

44

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri rapide Analyse de complexit


partition : calcul des sous-squences I/E/S en O(n) tri des squences I et S : meilleur des cas : 2*O(T(n/2)). pire des cas : 1+O(T(n-1)) concatnation I/E/S : parcours en une seule passe en O(n) meilleur des cas T(n) = 2*T(n/2) + O(n) T(n) = O(n log n) (thorme matre) 2 pire des cas T(n) = T(n-1) + O(n) = O(n2)
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Heap sort

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Description


On dispose dune squence S de n lments (1) Le tri par le tas commence par construire un tas depuis la squence trier (voir leon 13) (2) puis parcourt le tas, limine les racines du tas et les place la fin de la table trie
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Illustration


(1) squence initiale
[0] [1] [2] [3] [4]

(2) tas construit


[0] [1] [2] [3] [4]

13 73 29 41 30

73 41 29 13 30

13 73 41
Prof. Karim Bana
ENSIAS

73 29 41 13 30 29

30

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Illustration


(3) parcours et reconstruction du tas
[0] [1] [2] [3] [4] [0] [1] [2] [3]

73 41 29 13 30

41 30 29 13

73 41 13
[0] [1] [2]

41 29 30 13
[0] [1] [2] [3]

29
[4]

30 [3]

[4]

73

41 73

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Illustration


(3) parcours et reconstruction du tas
[0] [1] [2] [0] [1]

30 13 29

29 13

30 13
[0] [1] [2] [3]

29 29
[4] [0]

13
[1] [2] [3] [4]

30 41 73 Prof. Karim Bana


ENSIAS

29 30 41 73

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Illustration


(3) parcours et reconstruction du tas
[0]

13

13
(4) table trie
[0] [1] [2] [3] [4]

13 29 30 41 73 Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Implantation


void tri_par_le_tas (int *t, int taille) {
int debut = 0, fin = taille - 1 ; int debut = 0, fin = taille - 1 ; /* construction du tas intermdiaire initial */ /* construction du tas intermdiaire initial */ Tas * montas = Construire_tas_max (t, debut, fin); Tas * montas = Construire_tas_max (t, debut, fin); /* parcours/reconstruction du tas montas */ /* parcours/reconstruction du tas montas */ /* et mise jour trie de la table t */ /* et mise jour trie de la table t */ int ii = fin; int = fin; while(i>=0){ while(i>=0){ t[i--] = Rechercher_Maximum(montas); t[i--] = Rechercher_Maximum(montas); montas = Supprimer_Maximum(montas); } } montas = Supprimer_Maximum(montas); }

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Tri par le tas Analyse de complexit


construction du tas initial : positionner chacun des n lments en O(n log n) limination de la racine et reconstruction Supprimer_max en O(log n) parcours du tas jusqu vider le tas : n limination de la racine et reconstruction du tas O(n log n) T(1) = O(1), T(n) =O(n log n)
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Synthse

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Algorithmes avancs de tri Synthse


Le tri par le tas est le plus lent des tris avancs, mais ne requiert pas de rcursivit ni plusieurs tables intermdiaires Le tri par le tas est le plus attractif des trois pour les squences de millions dlments
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Algorithmes avancs de tri Synthse


Les tris avancs optimaux au pire des cas : sont le tri par le tas et le tri par partition-fusion Parmi les algorithmes de tri avancs tudis, le tri rapide est lun des plus optimaux en moyenne
Prof. Karim Bana
ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Supports utiliss

Prof. Karim Bana


ENSIAS

Leon n. 16 Algorithmes de Tri : Algorithmes avancs de tri

Programmation avance

Cours
P. Neophytou, Cours Heapsort , Pittsburgh University, USA, 2005 S. altenis, Cours Advanced Algorithms, Design and Analysis, Aalborg University, Denmark, 2003

Prof. Karim Bana


ENSIAS

S-ar putea să vă placă și