1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| Private Function TriRapide(Tableau() As Dossier, BorneInf As Long, BorneSup As Long) As Long
Dim IndexPivot As Long
Dim Pivot As Dossier
Dim Inf As Long
Dim Sup As Long
'Fin de la récursivité
If BorneInf >= BorneSup Then Exit Function
'Choix du pivot : pifomètre. La meilleur valeur étant la valeur médiane.
Randomize
IndexPivot = BorneInf + (BorneSup - BorneInf) * Rnd()
Pivot = Tableau(IndexPivot)
'Remontée de la première valeur
Tableau(IndexPivot) = Tableau(BorneInf)
'Initialisation des variables de parcours
Inf = BorneInf
Sup = BorneSup
Do
Do While Tableau(Sup).Taille >= Pivot.Taille
Sup = Sup - 1
If Sup <= Inf Then Exit Do
Loop
If Sup <= Inf Then
'Toutes les valeurs à gauche du pivot sont <= au pivot
'Toutes les valeurs à droite du pivot sont >= au pivot
Tableau(Inf) = Pivot
Exit Do
End If
Tableau(Inf) = Tableau(Sup)
Inf = Inf + 1
Do While Tableau(Inf).Taille < Pivot.Taille
Inf = Inf + 1
If Sup <= Inf Then Exit Do
Loop
If Sup <= Inf Then
Inf = Sup
Tableau(Sup) = Pivot
Exit Do
End If
Tableau(Sup) = Tableau(Inf)
Loop
TriRapide Tableau(), BorneInf, Inf - 1
TriRapide Tableau(), Sup + 1, BorneSup
End Function |
Partager