Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sort Array
Message
De
23/07/2001 12:00:54
Mark Hall
Independent Developer & Voip Specialist
Keston, Kent, Royaume Uni
 
 
Information générale
Forum:
Visual Basic
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
00533289
Message ID:
00533976
Vues:
13
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
Regards
Mark

Microsoft VFP MCP
Menulib - OO Menus for VFP www.hidb.com/menulib
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform