Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sort Array
Message
From
24/07/2001 10:31:46
 
 
To
23/07/2001 12:00:54
Mark Hall
Independent Developer & Voip Specialist
Keston, Kent, United Kingdom
General information
Forum:
Visual Basic
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00533289
Message ID:
00534392
Views:
19
Cool! I like it. Another UT victory.

>Hi,
>
>Here's a slightly modified version of Joes code, that implements the Bubblesort algorithm. This will sort 50,000 random 50 byte strings in < 1 second. I suppose it just depends on how much data you have, as to which you might use.
>
>I like Joes use of the CopyMemory function, I didn't test this just using VB to move the strings around, but it would probably be much slower.
>
>If you need even more speed, search for details on the QuickSort algorithm.
>
>
>Option Explicit
>Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cbCopy As Long)
>
>Public Function foo2()
>
>    Dim xx(50000) As String
>
>    Debug.Print "Generating Data"
>    PopulateArray xx
>
>    Debug.Print "Starting to sort", Now
>
>    BubbleSort xx
>
>    Debug.Print "Finish:", Now
>
>End Function
>
>Sub BubbleSort(varArray() As String)
>    Dim i As Long
>    Dim l_Count As Long
>    Dim l_Swap As Boolean
>
>    l_Count = UBound(varArray)
>
>    l_Swap = True
>    Do While l_Swap
>        For i = 0 To l_Count - 1
>            l_Swap = False
>            If varArray(i) > varArray(i + 1) Then
>                l_Swap = True
>                SwapStrings varArray(i), varArray(i + 1)
>            End If
>        Next
>    Loop
>End Sub
>
>Sub SwapStrings(pbString1 As String, pbString2 As String)
>    Dim l_Hold As Long
>    CopyMemory l_Hold, ByVal VarPtr(pbString1), 4
>    CopyMemory ByVal VarPtr(pbString1), ByVal VarPtr(pbString2), 4
>    CopyMemory ByVal VarPtr(pbString2), l_Hold, 4
>End Sub
>
>
>Sub PopulateArray(varArray() As String)
>    Dim i As Long, j As Long
>    Dim l_Count As Long, lcStr As String
>
>    ' Typical sorting routine
>    l_Count = UBound(varArray)
>    Randomize
>    lcStr = ""
>    For i = 0 To l_Count
>        lcStr = ""
>        For j = 1 To 50
>            lcStr = lcStr + Chr$((Rnd(1) * 26) + 65)
>        Next
>        varArray(i) = lcStr
>    Next
>End Sub
>
>
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Reply
Map
View

Click here to load this message in the networking platform