Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Dim aryI() as long
aryI = SortStrComp(mystrs,TextCompare)
For I = 0 to Ubound(aryI)
debug.print mystrs(aryI(I))
Next
Public Function SortStrComp(aryS() As String, Optional ByVal lngCompareType As CompareMethod) As Long()
' Sort the Array
Dim lngN As Long, _
lngGAP As Long, _
I As Long, _
J As Long, _
JGap As Long, _
lngI As Long, _
lngIGap As Long, _
lngBound As Long
Dim lngSwap As Long
Dim aryI() As Long
lngBound = UBound(aryS)
lngN = UBound(aryS) + 1 ' Get Actual Count including 0
ReDim aryI(lngBound) As Long
For I = 0 To lngN - 1
aryI(I) = I
Next
lngGAP = 1
Do While (lngGAP < lngN)
lngGAP = lngGAP * 3 + 1
Loop
lngGAP = (lngGAP - 1) \ 3
Do While lngGAP > 0
For I = lngGAP To lngN - 1
JGap = I
J = JGap - lngGAP
Do While J >= 0
lngI = aryI(J)
lngIGap = aryI(JGap)
Select Case StrComp(aryS(lngI), aryS(lngIGap), lngCompareType)
Case -1
Exit Do
Case 0
If lngI <= lngIGap Then
Exit Do
End If
End Select
aryI(J) = lngIGap
aryI(JGap) = lngI
JGap = J
J = J - lngGAP
Loop
Next
If lngGAP <= 1 Then Exit Do
lngGAP = (lngGAP - 1) \ 3
Loop
SortStrComp = aryI
Erase aryI
End Function
'* Inieffcient form
blnSwap = True
Do While(blnSwap)
' On each pass the largest of the remaining
' items, "bubbles" to its position
blnSwap = false
For I = LBound+1 To Ubound(Ary)
If Array(I-1) > Ary(I) then
strHold = Ary(I-1)
Ary(I-1) = Ary(I)
Ary(I) = strHold
blnSwap = true
End if
Next
Loop
'* Twice as fast
blnSwap = True
Do
For J = UBound(ary) to 0 Step -1
' On each pass the largest of the remaining
' items, "bubbles" to its position
blnswap = false
For I = LBound+1 To J
If Array(I-1) > Ary(I) then
strHold = Ary(I-1)
Ary(I-1) = Ary(I)
Ary(I) = strHold
blnSwap = true
End if
Next
if blnSwap = false then exit do
Next
Loop
Public Sub SortStrComp(aryS() As String, Optional ByVal lngCompareType As CompareMethod)
....sort sort sort
Dim aryW() as string
aryW = aryS
For I = 0 to Ubound(aryS)
aryS(I) = aryW(ary(I))
Next
End Sub
[code]
You still gain all the speed of not swapping strings.
Swapping strings is extremely slow because of all the memory allocation.
With mine, you get UBound allocations, with string swapping,
only God knows. I put in indexing after enduring string swapping. [URL unfurl="true"]WWW.VBCompare.Com[/URL]
' distance starts at 0
Do
distance = distance * 3 + 1
'IF numEls = 0 then distance = 1
Loop Until distance > numEls
Do
' If distance = 1 then distance = 0 afer following
distance = distance \ 3
For index = distance + 1 To numEls
..........
Next
' if distance = 0 then endless loop
Loop Until distance = 1