Test si un tableau est trié

Test si un tableau est trié

Messagepar Michael DELIQUE » 25 Fév 2008 à 09:48

Code : Tout sélectionner
Function isSorted( vrTable As Variant, nbCroissant As Integer) As Integer
   
   'Déclaration Variables
   
   Dim nbLBound As Long
   Dim nbUBound As Long
   Dim nbStep   As Long
   Dim i As Long
   
   On Error Goto ErreurHandle
   
   If Not Isarray(vrTable) Then
      isSorted = False
      Exit Function
   End If   
   
   If nbCroissant = True Then
      'ascendant
      nbStep = 1
      nbLBound = Lbound(vrTable)
      nbUBound = Ubound(vrTable)
   Else
      'descendant
      nbStep = -1
      nbLBound = Ubound(vrTable)
      nbUBound = Lbound(vrTable)
   End If
   
   If nbLBound = nbUBound Then
      isSorted = True
      Exit Function
   End If
   
   For i = nbLBound To nbUBound-1 Step nbStep
      If vrTable(i) > vrTable(i + nbStep) Then
         isSorted = False
         Exit Function
      End If   
   Next
   
   isSorted = True
   Exit Function
ErreurHandle:
   Msgbox "("+Cstr(Getthreadinfo (1))+" Call by "+Cstr(Getthreadinfo(10))+")"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"   
   Exit Function
End Function
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy

Retour vers Fonctions de tris