Option Base 1
Dim Q&, ch&, P&
'Tri par insertion (Insertion sort)
Sub testQSort()
Dim TB, TA(), SortColl As New Collection, GetTA, Tmps!
Cells(1, 3).Resize(10000).ClearContents
Q = 0: ch = 0: tim = Timer
TB = Cells(1).CurrentRegion.Value
For Each t In TB
ReDim NewTB(1 To 1)
L = Mid(t, 1, Len(t) - 1): S = Len(t): cle = L & "|" & S
On Error Resume Next
SortColl.Add IIf(SortColl.Count = 0, 1, SortColl.Count + 1), cle
If Err Then
GetTA = TA(SortColl(cle)): ReDim Preserve GetTA(1 To UBound(GetTA) + 1): GetTA(UBound(GetTA)) = t: TA(SortColl(cle)) = GetTA
Else
ReDim Preserve TA(1 To SortColl.Count): TA(SortColl(cle)) = Array(t)
End If
Next
For i = 1 To UBound(TA): TA(i) = SortInsertion1(TA(i)): Q = Q + 1:: Next
TA = SortInsertion1TB(TA)
ReDim TB(1 To 1)
n = 0
For Each arr In TA
Q = Q + 1
ReDim Preserve TB(1 To n + UBound(arr))
For i = 1 To UBound(arr)
Q = Q + 1
TB(i + n) = arr(i)
Next
n = UBound(TB)
Next
MsgBox Format(Timer - tim, "#0.00") & " sec" & vbCrLf & Q & " TOURS DE BOUCLE" & vbCrLf & ch & " INTERVERTIONS"
Cells(1, 3).Resize(UBound(TB)) = Application.Transpose(TB)
End Sub
Function SortInsertion1(t)
Dim temp, i&, a&
For i = LBound(t) + 1 To UBound(t)
For a = LBound(t) To i - 1
If t(i) < t(a) Then TP = t(i): t(i) = t(a): t(a) = TP: ch = ch + 1
Q = Q + 1
Next
Next
SortInsertion1 = t
End Function
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Function SortInsertion1TB(t)
Dim temp, i&, a&
For i = LBound(t) + 1 To UBound(t)
For a = LBound(t) To i - 1
If t(i)(1) < t(a)(1) Then TP = t(i): t(i) = t(a): t(a) = TP: ch = ch + 1
Q = Q + 1
Next
Next
SortInsertion1TB = t
End Function