Bem vindo ao meu blog, faça comentários ao meu código para tentarmos chegar a uma boa solução

quinta-feira, 12 de julho de 2007

Algoritmo BubbleSort

Public Function BubbleSortArray(ByVal NumericArray As Variant) _ As Variant
'RETURNS Array, or vbEmpty if there's an error'e.g., passed array contains elements that'can't be compared to each other, such as'objects
'will work when elements are all numbers or single characters'of the same case.
Dim vAns As VariantDim vTemp As VariantDim bSorted As BooleanDim lCtr As LongDim lCount As LongDim lStart As LongDim lPass as long
vAns = NumericArray If Not IsArray(vAns) Then

BubbleSortArray = vbEmpty
Exit Function
End If
On Error GoTo ErrorHandler
lStart = LBound(vAns)l

Count = UBound(vAns)lPass=0
bSorted = False

Do While Not bSorted
bSorted = True
For lCtr = lCount - 1 To lStart + lPass Step -1

If vAns(lCtr + 1) <>
DoEvents
bSorted = False
vTemp = vAns(lCtr)
vAns(lCtr) = vAns(lCtr + 1)
vAns(lCtr + 1) = vTemp
End If
Next lCtr
lPass = lPass + 1
Loop BubbleSortArray = vAns
Exit Function
ErrorHandler:BubbleSortArray = vbEmpty

Exit Function
End Function

Sem comentários: