T.Jackson

- 21st July 2007, 02:28

Number sorting algo that I spat out yesterday can sort 10,000 64 BIT numbers in 15 seconds on a PIII 866MHz. Considering the simplicity of it - this is fast! The code is in Visual Basic but shouldn't be too hard to port to PBP. I don't have time to do it at the moment, anyone up for it? If you feel that you have an even better solution, please post it here.

Private Function Sort_Numbers(ByRef Num_Array)

' (An easy to get your head around algo)

Dim Compare_A As Long

Dim Compare_B As Long

Dim Swap As Long

Dim Array_Pos As Long

Dim i As Long

Dim j As Long

Dim k As Long

i = UBound(Num_Array) ' Size / last index of array

k = i ' Copy of i used to show progress - indexes sorted thus far

Do Until i = 0 ' Loop until entire list has been sorted

Compare_A = Num_Array(i) ' Load comparison var (starting at bottom of list)

For j = Array_Pos To i ' Try to find a bigger num (starting from top of list)

Compare_B = Num_Array(j) '

If Compare_B > Compare_A Then ' This num bigger?

Swap = Num_Array(i) ' Swap them

Num_Array(i) = Compare_B '

Num_Array(j) = Swap

Array_Pos = j '

Exit For ' Bail out, but we need to come back (might be an even bigger num)

End If

Next

If j > i Then ' Num is in correct pos - nothing bigger exists

i = i - 1 ' Move to next index

Array_Pos = 0

' Show user how many have been sorted so far (yes this will slow things down marginally)

Progress = "Sorted " & (k - i) & " numbers"

DoEvents

End If

Loop

End Function

Private Function Sort_Numbers(ByRef Num_Array)

' (An easy to get your head around algo)

Dim Compare_A As Long

Dim Compare_B As Long

Dim Swap As Long

Dim Array_Pos As Long

Dim i As Long

Dim j As Long

Dim k As Long

i = UBound(Num_Array) ' Size / last index of array

k = i ' Copy of i used to show progress - indexes sorted thus far

Do Until i = 0 ' Loop until entire list has been sorted

Compare_A = Num_Array(i) ' Load comparison var (starting at bottom of list)

For j = Array_Pos To i ' Try to find a bigger num (starting from top of list)

Compare_B = Num_Array(j) '

If Compare_B > Compare_A Then ' This num bigger?

Swap = Num_Array(i) ' Swap them

Num_Array(i) = Compare_B '

Num_Array(j) = Swap

Array_Pos = j '

Exit For ' Bail out, but we need to come back (might be an even bigger num)

End If

Next

If j > i Then ' Num is in correct pos - nothing bigger exists

i = i - 1 ' Move to next index

Array_Pos = 0

' Show user how many have been sorted so far (yes this will slow things down marginally)

Progress = "Sorted " & (k - i) & " numbers"

DoEvents

End If

Loop

End Function