Winand

string array quick sort (alfa, len-down)

Aug 22nd, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes&)
  3. Private strings() As String
  4. Private Const SIZE = 30
  5. Private Const LENG = 10
  6.  
  7. Private Sub Form_Load()
  8.     Randomize
  9.     ReDim strings(SIZE) As String
  10.     Dim i As Long, j As Long, l_ As Long, t As String
  11.     Debug.Print "==Unsorted=="
  12.     For i = 0 To SIZE
  13.         l_ = Int(Rnd * LENG) + 1
  14.         For j = 1 To l_
  15.             t = t & Chr$(Int(Rnd * 58) + &H41)
  16.         Next j
  17.         strings(i) = t
  18.         t = vbNullString
  19.         Debug.Print l_, strings(i)
  20.     Next i
  21.     Debug.Print
  22.     Debug.Print "==Alfa-sorted=="
  23.     quickSort 0, SIZE, strings
  24.     For i = 0 To SIZE
  25.         Debug.Print Len(strings(i)), strings(i)
  26.     Next i
  27.     Debug.Print
  28.     Debug.Print "==Len-sorted=="
  29.     quickSort2 0, SIZE, strings
  30.     For i = 0 To SIZE
  31.         Debug.Print Len(strings(i)), strings(i)
  32.     Next i
  33. End Sub
  34.  
  35. Private Sub quickSort(ByVal First As Long, ByVal Last As Long, ByRef dat() As String)
  36.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
  37.    '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
  38.    Dim low As Long, high As Long, MidPrimary As String
  39.     low = First
  40.     high = Last
  41.     MidPrimary = dat((First + Last) \ 2)
  42.     Do
  43.         While StrComp(dat(low), MidPrimary, vbTextCompare) = -1
  44.             low = low + 1 '<
  45.        Wend
  46.         While StrComp(dat(high), MidPrimary, vbTextCompare) = 1
  47.             high = high - 1 '>
  48.        Wend
  49.         If low <= high Then
  50.             If low < high Then _
  51.                 SwapCells dat, low, high
  52.             low = low + 1
  53.             high = high - 1
  54.         End If
  55.     Loop While low <= high
  56.     If First < high Then quickSort First, high, dat
  57.     If low < Last Then quickSort low, Last, dat
  58. End Sub
  59.  
  60. Private Sub quickSort2(ByVal First As Long, ByVal Last As Long, ByRef dat() As String)
  61.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
  62.    '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
  63.    Dim low As Long, high As Long, MidPrimary As String
  64.     low = First
  65.     high = Last
  66.     MidPrimary = dat((First + Last) \ 2)
  67.     Do
  68.         Do
  69.             Select Case Len(MidPrimary) - Len(dat(low))
  70.             Case Is < 0: low = low + 1 '<
  71.            Case 0: '=
  72.                If StrComp(dat(low), MidPrimary, vbTextCompare) = -1 Then _
  73.                     low = low + 1 _
  74.                 Else Exit Do
  75.             Case Is > 0: Exit Do '>
  76.            End Select
  77.         Loop
  78.         Do
  79.             Select Case Len(MidPrimary) - Len(dat(high))
  80.             Case Is < 0: Exit Do '<
  81.            Case 0: '=
  82.                If StrComp(dat(high), MidPrimary, vbTextCompare) = 1 Then _
  83.                     high = high - 1 _
  84.                 Else Exit Do
  85.             Case Is > 0: high = high - 1 '>
  86.            End Select
  87.         Loop
  88.         If low <= high Then
  89.             If low < high Then _
  90.                 SwapCells dat, low, high
  91.             low = low + 1
  92.             high = high - 1
  93.         End If
  94.     Loop While low <= high
  95.     If First < high Then quickSort2 First, high, dat
  96.     If low < Last Then quickSort2 low, Last, dat
  97. End Sub
  98.  
  99. 'swap cells in 1D string array
  100. Private Sub SwapCells(ByRef arr() As String, ByVal i As Long, ByVal j As Long)
  101.     Dim tmp As Long
  102.     CopyMemory tmp, ByVal VarPtr(arr(i)), 4
  103.     CopyMemory ByVal VarPtr(arr(i)), ByVal VarPtr(arr(j)), 4
  104.     CopyMemory ByVal VarPtr(arr(j)), tmp, 4
  105. End Sub
Advertisement
Add Comment
Please, Sign In to add comment