Chapitre IV Algorithmes de Tri
Chapitre IV Algorithmes de Tri
CHAPITRE IV:
ALGORITHMES DE TRI
Cours n°7: 30 Octobre 2013
AROUSSI Sana
Tri Rapide
Conclusion
INTRODUCTION
7 3 18 13 7 7 3 18 13 7 3 7 18 13 7
3 7 7 13 18 3 7 7 13 18 3 7 18 13 7
3 7 7 13 18 4
TRI PAR SÉLECTION
ALGORITHME RÉCURSIF
Debut
FSI
Fin 5
TRI PAR SÉLECTION
FONCTION « RECHERCHER_MIN »
Debut
min debut
Rechercher_min min;
Fin
6
TRI PAR SÉLECTION
COMPLEXITÉ DE LA FONCTION « RECHERCHER_MIN »
Debut
min debut C1
Rechercher_min min; C3
Fin
FSI
3 7 7 13 18 3 7 7 13 18 3 7 13 18 7
9
TRI PAR INSERTION
ALGORITHME RÉCURSIF
Debut
Si (n>1) alors
Tri_Insertion(T,n-1);
FSI
Fin
10
TRI PAR INSERTION
PROCÉDURE « INSÉRER »
Fin
TRI PAR INSERTION
COMPLEXITÉ DE LA PROCÉDURE « INSÉRER »
Debut
Si (n>1) alors
Tri_Insertion(T,n-1); T (n - 1)
FSI
Fin 13
T (n) = T(n-1) + Tinsérer (n ) O(T) = O (n2)
TRI PAR PROPAGATION
PRINCIPE
L'algorithme de tri par propagation (ou à bulles) consiste à
faire remonter progressivement les plus grands éléments d'un
tableau. Son principe est d’inverser deux éléments successifs
s'ils ne sont pas classés dans le bon ordre et de recommencer
jusqu'à ce qu’on ne peut plus permuter.
7 3 18 13 7 3 7 18 13 7 3 7 18 13 7
3 7 13 7 18 3 7 13 7 18 3 7 13 18 7
14
3 7 13 7 18 3 7 7 13 18 3 7 7 13 18
TRI PAR PROPAGATION
ALGORITHME RÉCURSIF
Fin
TRI PAR PROPAGATION
FONCTION « TRIER »
Fin
TRI PAR PROPAGATION
COMPLEXITÉ DE LA FONCTION « TRIER »
FSI 18
7 3 18 13 7
7 3 18 13 7
13 7
3 7
7 13
7 13 18
19
3 7 7 10 13 18
TRI PAR FUSION
PARADIGME DIVISER POUR RÉGNER
20
TRI PAR FUSION
ALGORITHME RÉCURSIF
Debut
Si (debut<fin) alors
FSI 21
Fin
TRI PAR FUSION
PROCÉDURE « FUSIONNER »
Procédure fusionner(VAR T: tableau, debut, milieu, fin: entier)
Debut
Tmp: tableau temporaire du taille fin-debut+1
i debut; gauche debut, droite milieu + 1;
Tant que (i<=fin) faire
Si ((gauche<=milieu et T[gauche]<T[droite]) ou droite>fin) alors
Tmp[i]T[gauche]; gauche++;
Sinon
Tmp [i]T[droite]; droite++;
// recopier le tableau
Pour idebut à fin faire T[i]=tmp[i]
22
Fin
TRI PAR FUSION
COMPLEXITÉ DE LA PROCÉDURE « FUSIONNER »
Procédure fusionner(VAR T: tableau, debut, milieu, fin: entier)
Debut Tfusionner(n) tq n= fin – debut +1
Si (debut<fin) alors
FSI 24
CHAPITRE IV:
ALGORITHMES DE TRI
Cours n°8: 3 Novembre 2013
AROUSSI Sana
26
TRI RAPIDE
PARADIGME DIVISER POUR RÉGNER
27
TRI RAPIDE
ALGORITHME RÉCURSIF
Debut
FSI
Fin 28
TRI RAPIDE
FONCTION « PARTITIONNER »
Partitionner (T: tableau, debut, fin, pivot: entier): entier
Debut
permuter (T[pivot], T[fin])
j premier
Pour i 1 à fin -1 faire
SI T[i] <= T[fin] alors
Permuter (T[i], T[j])
jj+1
FSI
FP
Permuter (T[fin] , T[j])
29
Partitionner j
Fin
TRI RAPIDE
FONCTION « PARTITIONNER »
Partitionner (T: tableau, debut, fin, pivot: entier): entier
Debut
permuter (T[pivot], T[fin])
j premier
Pour i 1 à fin -1 faire
SI T[i] <= T[fin] alors
Permuter (T[i], T[j])
jj+1
FSI
FP
Permuter (T[fin] , T[j])
30
Partitionner j
Fin
TRI RAPIDE
COMPLEXITÉ DE LA FONCTION « PARTITIONNER »
Partitionner (T: tableau, debut, fin, pivot: entier): entier
Debut Tpartitionner(n) tq n= fin – debut +1
permuter (T[pivot], T[fin])
j premier
Pour i 1 à fin -1 faire Pire de cas: n-1 fois
Debut
FSI
Fin 32
T (n) = T(pivot-debut+1) + T(fin-pivot) + Tpartitionner(n)
TRI RAPIDE
CHOIX DU PIVOT ET COMPLEXITÉ
région à un élément:
O(T) = O(n2)
taille n/2 :
34
Université Saad Dahleb de Blida
Faculté des Sciences
Département d’Informatique
Licence Génie des Systèmes Informatique (GSI)
Semestre 5 (3ème année)
ALGORITHMIQUE 02
CHAPITRE IV:
ALGORITHMES DE TRI
Cours n°9: 10 Novembre 2013
AROUSSI Sana
36
TRI PAR ABR
PRINCIPE
20 15 10 35 19 13 5 3 12 7 16 40 25 38
20
15 35
10 25 40
19
5 13 38
37
16
3 7 12
TRI PAR ABR
PRINCIPE
15 35
10 25 40
19
5 13 38
16
3 7 12
3 5 7 10 12 13 15 16 19 20 25 35 38 40 38
TRI PAR ABR
ALGORITHME ITÉRATIF
Soit AR un arbre de recherche binaire
Debut
// Construire ARB
ARNil
Fin
TRI PAR ABR
FONCTION « INSÉRER »
Insérer (AR: nœud , x: entier): nœud
Debut
SINON
SI x ≤ valeur(AR) alors
SINON
FSI
40
Insérer AR
Fin
TRI PAR ABR
TERMINOLOGIE
h=0 20
h=1 15 35
h=4
10 19 25 40
h=2
n = 14
h=3 5 13 16 38
h=4 3 7 12 41
TRI PAR ABR
COMPLEXITÉ DE LA FONCTION « INSÉRER »
Insérer (AR: nœud , x: entier): nœud
Tinsérer (h) tq h est la hauteur de l’arbre
Debut
SINON
SI x ≤ valeur(AR) alors
SINON
Insérer AR 42
O(Tinsérer) = O (h)
h=0 25
h=1 18
25 18 15 5 2
h=2 15
h=3 5 43
h=4 2
TRI PAR ABR
PARCOURS INFIXÉ
Debut
Parcours_Infixe(FG(AR, T, indice))
Indice indice + 1;
Parcours_Infixe(FD(AR), T, indice)
FSI
44
Fin
TRI PAR ABR
COMPLEXITÉ DU PARCOURS INFIXÉ
Indice indice + 1;
FSI
45
Fin Tinfixe (n) = Tinfixe (k) + Tinfixe (n-k-1) + c
TRI PAR ABR
COMPLEXITÉ DU PARCOURS INFIXÉ
Tinfixe (n) = Tinfixe (k) + Tinfixe (n-k-1) + c
Évidemment, le parcours infixé passe par tous les nœuds de l’arbre, ainsi
O(Tinfixe) = O(n)
Cas général:
// Construire ARB
O(TARB)= n * O (n) + O (n)
ARNil = O (n2) + O(n)
n fois = O(n2)
Pour i1 à n faire
Fin
TRI PAR ABR
ALGORITHME RÉCURSIF
Debut
SI (n>0) alors
FSI 48
Fin
TRI PAR ABR
COMPLEXITÉ DE L’ALGORITHME RÉCURSIF
Debut
SI (n>0) alors
FSI 49
CHAPITRE IV:
ALGORITHMES DE TRI
Cours n°10: 17 Novembre 2013
AROUSSI Sana
Propriété d’ordre :
4 Minimum
5 6
15 9 7 20
16 25 14 12 11 8
Maximum
52
TRI PAR TAS
TASMAX
40 Maximum
35 26
15 19 17 20
1 13 14 12 11 8
Maximum
53
TRI PAR TAS
HAUTEUR D’UN TAS
Théorème: Un tas de n nœud a une hauteur O(log2 n)
Démonstration
h=0 ; n0 = 20
h=1 ; n1 = 21
h=2 ; n2 = 22
54
h=3 ; n3 = 23
TRI PAR TAS
HAUTEUR D’UN TAS
Théorème: Un tas de n nœud a une hauteur O(log2 n)
Démonstration
1 4
2 3
6
5
4 5 6 7
15 9 7 20
8 9
16 25 14 12 11 8
56
1 2 3 4 5 6 7 8 9 10 11 12 13
4 5 6 15 9 7 20 16 25 14 12 11 8
TRI PAR TAS
REPRÉSENTATION PAR TABLEAU
Indice(racine)=1
Indice(FG)=2*Indice(Père)
Indice(FD)=2*Indice(Père)+1
1 2 3 4 5 6 7 8 9 10 11 12 13
4 5 6 15 9 7 20 16 25 14 12 11 8
57
TRI PAR TASMIN
INSERTION
Pour insérer une valeur « v » dans un TASmin
1. Insérer « v » à la fin du dernier niveau de l’arbre (à la fin du
tableau).
Exemple: insertion de 3
4
6
5
15 9 7 20
16 25 14 12 11 8
58
4 5 6 15 9 7 20 16 25 14 12 11 8
4 4
6 6
5 5
15 9 7 20 15 9 7 3
16 25 14 12 11 8 3 16 25 14 12 11 8 20
4 5 6 15 9 7 20 16 25 14 12 11 8 3 4 5 6 15 9 7 3 16 25 14 12 11 8 20
3
4
4
5 3
5
15 9 7 6
15 9 7 6
16 25 14 12 11 8 20
16 25 14 12 11 8 20
3 5 4 15 9 7 6 16 25 14 12 11 8 20
4 5 3 15 9 7 6 16 25 14 12 11 8 20
TRI PAR TASMIN
INSERTION
Procédure Insérer_TAS (Tas: tableau, n, x: entier)
Début
nn+1
in
Tas[i ] x
i i/2
60
Fin
TRI PAR TASMIN
EXTRAIRE MINIMUM
Le minimum se trouve à la racine.
Exemple :
4
6
5
15 9 7 20
16 25 14 12 11 26 61
4 5 6 15 9 7 20 16 25 14 12 11 8
4 26
6 6
5 5
15 9 7 20 15 9 7 20
16 25 14 12 11 26 16 25 14 12 11
4 5 6 15 9 7 20 16 25 14 12 11 26 26 5 6 15 9 7 20 16 25 14 12 11
5 5
6 6
9 26
15 26 7 20 15 9 7 20
16 25 14 12 11 16 25 14 12 11
5 9 6 15 26 7 20 16 25 14 12 11 5 26 6 15 9 7 20 16 25 14 12 11 62
5 5
6 6
9 26
15 26 7 20 15 9 7 20
16 25 14 12 11 16 25 14 12 11
5 9 6 15 26 7 20 16 25 14 12 11 5 26 6 15 9 7 20 16 25 14 12 11
6
9
15 26 7 20
16 25 14 26 11
5 9 6 15 12 7 20 16 25 14 26 11
63
TRI PAR TASMIN
EXTRAIRE MINIMUM
ExtraireMin (Tas: tableau, n : entier )
Début
i min; g 2* i ; d 2 *i + 1
Fin
TRI PAR TASMIN
PRINCIPE
20 15 10 35 19 13 5 3 12 7 16 40 25 38
20 15 10 10
15 20 20 19
10 15 15
35 19 35 20 13
10
3
19 13
5 10
5
35 20 15 5
19 19 10
20 15 13
35
65
12 35 20 15 13
3
TRI PAR TASMIN
PRINCIPE
20 15 10 35 19 13 5 3 12 7 16 40 25 38
3 3
5 10 5 10
12 20 15 13 12 7 15 13
35 19 35 19
7 20 16 40 25 38
66
TRI PAR TASMIN
PRINCIPE
5 10
12 7 15 13 5
35 19 7 10
20 16 40 25 38
12 16 15 13
35 19 20 38 40 25
3 5 67
TRI PAR TASMIN
PRINCIPE
12 10
19 16 15 13
10
35 25 20 38 40
12 13
19 16 15 40
35 25 20 38
3 5 7 10 68
TRI PAR TASMIN
PRINCIPE
16 15
16 13
19 20 38 40
19 20 15 40
35 25
35 25 38
15
16 25
19 20 38 40
35
69
3 5 7 10 12 13 15
TRI PAR TASMIN
PRINCIPE
16 19
19 25 20 25
35 20 38 40 35 40 38
35 25 20
38 40 35 40 35 25
38 38 40
38
40
40
70
3 5 7 10 12 13 15 16 19 20 25 35 38 40
TRI PAR TASMIN
ALGORITHME ITÉRATIF
Tri_TASmin (T: Tableau, n: entier)
Début
//Construire le TAS
Pour i 1 à n faire
Insérer_TAS (Tas, i-1, T[i])
// Extraire les minimums
Pour i1 à n faire
T[i] TAS[1]
Extraire_Minimum (TAS, n)
Fin 71
TRI PAR TASMIN
COMPLEXITÉ DE L’ALGORITHME ITÉRATIF
Tri_TASmin (T: Tableau, n: entier) TTAS (n) tq n la taille du tableau
Début
//Construire le TAS
Pour i 1 à n faire
Insérer_TAS (Tas, i-1, T[i]) TInsérer ??
Début
i i/2
73
Fin
TInsérer (n) = c1 log2(n+1) + c2 O(TInsérer ) = O(log2(n+1) )
TRI PAR TASMIN
COMPLEXITÉ DE LA PROCÉDURE EXTRAIRE_MINIMUM
Extraire_Minimum (Tas: tableau, n : entier ) TExtraire(n)
Début
Si d < n et Tas[d] < Tas[min] alors min d l’arbre donc égale à log2 n
O(TTAS ) = O(nlog2(n) )
ALGORITHME RÉCURSIF
i1; phase1
Tri_TASmin (T, TAS: Tableau, n, i, phase: entier)
Début
Si Phase = 1 alors //Construire le TAS
Si (i<=n) alors
Insérer_TAS (Tas, i-1, T[i])
Tri_TASmin (T, TAS, i++, n, phase)
Sinon
Tri_TASmin(T, TAS, 0, n, 2) // On passe à la phase 2
Sinon // Phase 2: Extraire les minimums
Si (i<n) alors
T[i+1] TAS[1]
Extraire_Minimum (TAS, n-i)
Tri_TASmin (T, TAS, i++, n, phase)
76
Fsi
Fin
COMPLEXITÉ DE L’ALGORITHME RÉCURSIF
i1; phase1
Tri_TASmin (T, TAS: Tableau, n, i, phase: entier)
Début TTAS (m) tq m= n-i+1 la taille effective du tableau (m n)
79
TP: TRI PAR ABRE
Un Arbre Binaire de Recherche Équilibré (ABRE), comme son nom
indique, est un arbre :
Dans TAS, l'arbre est complet, i.e. si sa hauteur est h, les niveaux
inférieurs (0, 1, 2,...., h-1) sont entièrement remplis.
.............
TP: TRI PAR ABRE
Le but de ce TP est de proposer un algorithme de tri par un ABRE
permettant de classer les éléments du tableau.
1. Conception de l’algorithme
2. Implémentation de l’algorithme
François Laroussinie Algorithmes de tri, Université Paris Diderot (Paris 7), 2010, pp
110. Disponible sur www.liafa.jussieu.fr/~francoisl/IREM/tri.pdf