Page 1 sur 1

Test si un tableau est trié

MessagePublié: 25 Fév 2008 à 09:48
par Michael DELIQUE
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