Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes&)
- Private strings() As String
- Private Const SIZE = 30
- Private Const LENG = 10
- Private Sub Form_Load()
- Randomize
- ReDim strings(SIZE) As String
- Dim i As Long, j As Long, l_ As Long, t As String
- Debug.Print "==Unsorted=="
- For i = 0 To SIZE
- l_ = Int(Rnd * LENG) + 1
- For j = 1 To l_
- t = t & Chr$(Int(Rnd * 58) + &H41)
- Next j
- strings(i) = t
- t = vbNullString
- Debug.Print l_, strings(i)
- Next i
- Debug.Print
- Debug.Print "==Alfa-sorted=="
- quickSort 0, SIZE, strings
- For i = 0 To SIZE
- Debug.Print Len(strings(i)), strings(i)
- Next i
- Debug.Print
- Debug.Print "==Len-sorted=="
- quickSort2 0, SIZE, strings
- For i = 0 To SIZE
- Debug.Print Len(strings(i)), strings(i)
- Next i
- End Sub
- Private Sub quickSort(ByVal First As Long, ByVal Last As Long, ByRef dat() As String)
- ' Made by Michael Ciurescu (CVMichael from vbforums.com)
- ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
- Dim low As Long, high As Long, MidPrimary As String
- low = First
- high = Last
- MidPrimary = dat((First + Last) \ 2)
- Do
- While StrComp(dat(low), MidPrimary, vbTextCompare) = -1
- low = low + 1 '<
- Wend
- While StrComp(dat(high), MidPrimary, vbTextCompare) = 1
- high = high - 1 '>
- Wend
- If low <= high Then
- If low < high Then _
- SwapCells dat, low, high
- low = low + 1
- high = high - 1
- End If
- Loop While low <= high
- If First < high Then quickSort First, high, dat
- If low < Last Then quickSort low, Last, dat
- End Sub
- Private Sub quickSort2(ByVal First As Long, ByVal Last As Long, ByRef dat() As String)
- ' Made by Michael Ciurescu (CVMichael from vbforums.com)
- ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
- Dim low As Long, high As Long, MidPrimary As String
- low = First
- high = Last
- MidPrimary = dat((First + Last) \ 2)
- Do
- Do
- Select Case Len(MidPrimary) - Len(dat(low))
- Case Is < 0: low = low + 1 '<
- Case 0: '=
- If StrComp(dat(low), MidPrimary, vbTextCompare) = -1 Then _
- low = low + 1 _
- Else Exit Do
- Case Is > 0: Exit Do '>
- End Select
- Loop
- Do
- Select Case Len(MidPrimary) - Len(dat(high))
- Case Is < 0: Exit Do '<
- Case 0: '=
- If StrComp(dat(high), MidPrimary, vbTextCompare) = 1 Then _
- high = high - 1 _
- Else Exit Do
- Case Is > 0: high = high - 1 '>
- End Select
- Loop
- If low <= high Then
- If low < high Then _
- SwapCells dat, low, high
- low = low + 1
- high = high - 1
- End If
- Loop While low <= high
- If First < high Then quickSort2 First, high, dat
- If low < Last Then quickSort2 low, Last, dat
- End Sub
- 'swap cells in 1D string array
- Private Sub SwapCells(ByRef arr() As String, ByVal i As Long, ByVal j As Long)
- Dim tmp As Long
- CopyMemory tmp, ByVal VarPtr(arr(i)), 4
- CopyMemory ByVal VarPtr(arr(i)), ByVal VarPtr(arr(j)), 4
- CopyMemory ByVal VarPtr(arr(j)), tmp, 4
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment