Trier 6 chiffres en ordre croissant

cs_SirJack Messages postés 9 Date d'inscription vendredi 14 février 2003 Statut Membre Dernière intervention 17 avril 2003 - 18 mars 2003 à 23:26
davidauche Messages postés 150 Date d'inscription jeudi 20 mars 2003 Statut Membre Dernière intervention 8 janvier 2008 - 4 juin 2005 à 06:34
Je fais un jeux de loto et j'ai besoin de trier 6 chiffres en ordre croissant.

Montrer moi n,importe quel moyen s.v.p.

En étant très reconnaissant, Sir Jack
A voir également:

4 réponses

fredlynx Messages postés 662 Date d'inscription mercredi 16 janvier 2002 Statut Modérateur Dernière intervention 16 octobre 2010 3
19 mars 2003 à 02:05
Un exemple de trie d'un tableau.... Un commandbutton donne ordre de trier un tableau de façon croisante ou décroissante au choix...

Private Sub Command1_Click()
Dim TabChiffre(5) As Integer
TabChiffre(0) = 8
TabChiffre(1) = 5
TabChiffre(2) = 40
TabChiffre(3) = 17
TabChiffre(4) = 2
TabChiffre(5) = 38

TrieTable TabChiffre(), True

End Sub

Private Function TrieTable(TabChiffre() As Integer, Croissant As Boolean)
Dim Boucle As Integer
Dim Vartmp As Integer
Boucle = 0
Do
Boucle = Boucle + 1
If (TabChiffre(Boucle) < TabChiffre(Boucle - 1)) And (Croissant = True) Then
Vartmp = TabChiffre(Boucle - 1)
TabChiffre(Boucle - 1) = TabChiffre(Boucle)
TabChiffre(Boucle) = Vartmp
Boucle = 0
ElseIf (TabChiffre(Boucle) > TabChiffre(Boucle - 1)) And (Croissant = False) Then
Vartmp = TabChiffre(Boucle - 1)
TabChiffre(Boucle - 1) = TabChiffre(Boucle)
TabChiffre(Boucle) = Vartmp
Boucle = 0
End If
Loop While Boucle < 5
End Function

<center>http://www.fredlynx.com
</center>
0
pcpunch Messages postés 1243 Date d'inscription mardi 7 mai 2002 Statut Membre Dernière intervention 18 février 2019 5
19 mars 2003 à 02:13
slt

je suis debutant et j ai plancher sur la question, mais g pas était aussi rapide que fredlynx, mais bon je te met qd mm mon exemple. G pris une source de trie a bulle du site.

Tu place un bouton de command1 et 6 textbox(1 à 6) pour entrer tes nombres..

Dim TABLEAU(6) As Integer 'declare tableau avec 6 enregistrements
Dim BOL As Boolean

Private Sub Command1_Click()
'On entre la valeur des textbox ds le tableau
TABLEAU(1) = Text1.Text
TABLEAU(2) = Text2.Text
TABLEAU(3) = Text3.Text
TABLEAU(4) = Text4.Text
TABLEAU(5) = Text5.Text
TABLEAU(6) = Text6.Text
' TRI A BULLE du tableau dans l ordre croissant
For j = 1 To 6
DoEvents
BOL = False
For i = 1 To (6 - j)
If TABLEAU(i) > TABLEAU(i + 1) Then
BOL = True
Temp = TABLEAU(i)
TABLEAU(i) = TABLEAU(i + 1)
TABLEAU(i + 1) = Temp
End If
Next
If Not BOL Then j = 6
Next
'on affiche le resultat du tableau trié ds les textbox
Text1.Text = TABLEAU(1)
Text2.Text = TABLEAU(2)
Text3.Text = TABLEAU(3)
Text4.Text = TABLEAU(4)
Text5.Text = TABLEAU(5)
Text6.Text = TABLEAU(6)
End Sub
0
davidauche Messages postés 150 Date d'inscription jeudi 20 mars 2003 Statut Membre Dernière intervention 8 janvier 2008
4 juin 2005 à 06:31
c'est juste pour corriger pcpunch, mais la méthode est parfaite.
pcpunch, quand tu declare un tableau(6) t'as 7 élements, ce n'est pas
le cas. les tableaux commencent par l'indice 0 et non
1.



voila le code après qlq modification. une procedure tri peut etre utiliser pour des autres tableaux avec n'importe taille.



Dim tableau(5) As Integer

Private Sub Command1_Click()



'On entre la valeur des textbox ds le tableau

tableau(0) = Text1.Text

tableau(1) = Text2.Text

tableau(2) = Text3.Text

tableau(3) = Text4.Text

tableau(4) = Text5.Text

tableau(5) = Text6.Text

' TRI A BULLE du tableau dans l ordre croissant

Call tri(tableau(), 6)

'on affiche le resultat du tableau trié ds les textbox

Text1.Text = tableau(0)

Text2.Text = tableau(1)

Text3.Text = tableau(2)

Text4.Text = tableau(3)

Text5.Text = tableau(4)

Text6.Text = tableau(5)

End Sub

Private Sub tri(t() As Integer, taille As Integer)

Dim temp, i, j As Integer

Dim BOL As Boolean



For j = 0 To taille - 1

DoEvents

BOL = False

For i = 0 To (taille - 2 - j)


If tableau(i) > tableau(i + 1) Then

BOL = True


temp = tableau(i)


tableau(i) = tableau(i + 1)


tableau(i + 1) = temp

End If

Next

If Not BOL Then j = taille - 1

Next



End Sub



go+

;)
0
davidauche Messages postés 150 Date d'inscription jeudi 20 mars 2003 Statut Membre Dernière intervention 8 janvier 2008
4 juin 2005 à 06:34
j'ai oublié qlq chose: la complexité de cette methode est enorme par
rapport a des autres algorithmes!!. mais bon! les vbistes s'en fou de
la complexité et la gestion de la mémoire.

l'important que ça marche...

:) ;)
0
Rejoignez-nous