View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Calling Bubble Sort Function


Some comments on the advice so far:
a bubble sort is one of the slowest sort algorithms
for the "i" loop you want to go to last - 1
You don't want to use the number of items in the array - you want to look at
the lower and upper bounds.
you can't assume list has a lower bound of 0


Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Long, Last As Long
Dim i As Long, j As Long
Dim Temp As Long

First = LBound(List)
Last = UBound(List)
For i = First To Last - 1
For j = i + 1 To Last
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i

End Function

--
Regards,
Tom Ogilvy


wrote in message
om...
I have an 1D array that I have filled within a For Next Loop. There
are roughly 60,000 values within the array. Upon filling the array, I
want to sort the data within the array. The array is called
CalculationArray. When I call the bubble sort function, it fails
within the function and gives a type mismatch error. (It fails on "For
i = 1 To List - 1").

Why is this so?


'Call Sort Function and Sort Bidding Array

BubbleSort CalculationArray

Function BubbleSort(List As Variant)
' Sorts an array using bubble sort algorithm
Dim First As Long, Last As Long
Dim i As Long, j As Long
Dim Temp As Long

First = LBound(List)
Last = UBound(List)
For i = 1 To List - 1 'THIS IS WHERE IT FAILS
For j = i + 1 To List
If List(i) List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i

End Function

Thanks

TS