Results 1 to 25 of 25

Thread: VB - Quick Sort algorithm (very fast sorting algorithm)

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    VB - Quick Sort algorithm (very fast sorting algorithm)

    The example is sorting string arrays, but you can modify it to use any type of arrays, just replace "As String" to the type you need...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim MyStrArray() As String, K As Long, Q As Long
    5.     ReDim MyStrArray(1 To 10)
    6.    
    7.     Randomize
    8.    
    9.     Debug.Print "Unsorted strings:"
    10.     For K = LBound(MyStrArray) To UBound(MyStrArray)
    11.        
    12.         ' create a random string
    13.         MyStrArray(K) = String(10, " ")
    14.         For Q = 1 To 10
    15.             Mid$(MyStrArray(K), Q, 1) = Chr(Asc("A") + Fix(26 * Rnd))
    16.         Next Q
    17.        
    18.         ' print the string to the immediate window
    19.         Debug.Print MyStrArray(K)
    20.     Next K
    21.    
    22.     ' sort the array
    23.     QuickSort MyStrArray, LBound(MyStrArray), UBound(MyStrArray)
    24.    
    25.     ' print the sorted string to the immediate window
    26.     Debug.Print vbNewLine & "Sorted strings:"
    27.     For K = LBound(MyStrArray) To UBound(MyStrArray)
    28.         Debug.Print MyStrArray(K)
    29.     Next K
    30. End Sub
    31.  
    32. Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long)
    33.     '
    34.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    35.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
    36.     '
    37.     Dim Low As Long, High As Long
    38.     Dim MidValue As String
    39.    
    40.     Low = First
    41.     High = Last
    42.     MidValue = C((First + Last) \ 2)
    43.    
    44.     Do
    45.         While C(Low) < MidValue
    46.             Low = Low + 1
    47.         Wend
    48.        
    49.         While C(High) > MidValue
    50.             High = High - 1
    51.         Wend
    52.        
    53.         If Low <= High Then
    54.             Swap C(Low), C(High)
    55.             Low = Low + 1
    56.             High = High - 1
    57.         End If
    58.     Loop While Low <= High
    59.    
    60.     If First < High Then QuickSort C, First, High
    61.     If Low < Last Then QuickSort C, Low, Last
    62. End Sub
    63.  
    64. Private Sub Swap(ByRef A As String, ByRef B As String)
    65.     Dim T As String
    66.    
    67.     T = A
    68.     A = B
    69.     B = T
    70. End Sub
    Last edited by CVMichael; Nov 30th, 2005 at 11:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width