codice:
Public Sub MergeSort(sArray() As Single, ByVal beginning As Long, ByVal ending As Long)
Dim middle As Long
If (ending > beginning) Then
' Recursively sort the two halves of the list.
middle = (beginning + ending) \ 2
MergeSort sArray, beginning, middle
MergeSort sArray, middle + 1, ending
' Merge the results.
Merge sArray, beginning, middle, ending
End If
End Sub
Public Sub Merge(sArray() As Single, ByVal beginning As Long, ByVal middle As Long, ByVal ending As Long)
Dim temp_array() As Single
Dim i As Long
Dim temp As Long
Dim counterA As Long
Dim counterB As Long
Dim counterMain As Long
Dim base As Integer
' Copy the array into a temporary array.
ReDim temp_array(beginning To ending, 6)
For i = beginning To ending
temp_array(i, 0) = sArray(i, 0)
temp_array(i, 1) = sArray(i, 1)
temp_array(i, 2) = sArray(i, 2)
temp_array(i, 3) = sArray(i, 3)
temp_array(i, 4) = sArray(i, 4)
temp_array(i, 5) = sArray(i, 5)
temp_array(i, 6) = sArray(i, 6)
Next
If (baseordin = True) Then
base = 1
Else
base = 5
End If
' counterA and counterB mark the next item to save
' in the first and second halves of the list.
counterA = beginning
counterB = middle + 1
' counterMain is the index where we will put the
' next item in the merged list.
counterMain = beginning
Do While (counterA <= middle) And (counterB <= ending)
' Find the smaller of the two items at the front
' of the two sublists.
If (temp_array(counterA, base) >= temp_array(counterB, base)) Then
' The smaller value is in the first half.
sArray(counterMain, 0) = temp_array(counterA, 0)
sArray(counterMain, 1) = temp_array(counterA, 1)
sArray(counterMain, 2) = temp_array(counterA, 2)
sArray(counterMain, 3) = temp_array(counterA, 3)
sArray(counterMain, 4) = temp_array(counterA, 4)
sArray(counterMain, 5) = temp_array(counterA, 5)
sArray(counterMain, 6) = temp_array(counterA, 6)
counterA = counterA + 1
'ProgressBar1.Value = ProgressBar1.Value + 0.22
Else
' The smaller value is in the second half.
sArray(counterMain, 0) = temp_array(counterB, 0)
sArray(counterMain, 1) = temp_array(counterB, 1)
sArray(counterMain, 2) = temp_array(counterB, 2)
sArray(counterMain, 3) = temp_array(counterB, 3)
sArray(counterMain, 4) = temp_array(counterB, 4)
sArray(counterMain, 5) = temp_array(counterB, 5)
sArray(counterMain, 6) = temp_array(counterB, 6)
counterB = counterB + 1
'ProgressBar1.Value = ProgressBar1.Value + 0.22
End If
counterMain = counterMain + 1
Loop
' Copy any remaining items from the first half.
Do While (counterA <= middle)
sArray(counterMain, 0) = temp_array(counterA, 0)
sArray(counterMain, 1) = temp_array(counterA, 1)
sArray(counterMain, 2) = temp_array(counterA, 2)
sArray(counterMain, 3) = temp_array(counterA, 3)
sArray(counterMain, 4) = temp_array(counterA, 4)
sArray(counterMain, 5) = temp_array(counterA, 5)
sArray(counterMain, 6) = temp_array(counterA, 6)
counterA = counterA + 1
counterMain = counterMain + 1
'ProgressBar1.Value = ProgressBar1.Value + 0.22
Loop
' Copy any remaining items from the second half.
Do While (counterB <= ending)
sArray(counterMain, 0) = temp_array(counterB, 0)
sArray(counterMain, 1) = temp_array(counterB, 1)
sArray(counterMain, 2) = temp_array(counterB, 2)
sArray(counterMain, 3) = temp_array(counterB, 3)
sArray(counterMain, 4) = temp_array(counterB, 4)
sArray(counterMain, 5) = temp_array(counterB, 5)
sArray(counterMain, 6) = temp_array(counterB, 6)
counterB = counterB + 1
counterMain = counterMain + 1
'ProgressBar1.Value = ProgressBar1.Value + 0.22
Loop
End Sub