|
最近做个排序,想用归并排序来做,结果发现程序老是出现下标超限,实在不知道什么原因,还请大神帮忙解答下,以下是代码:
Sub test()
a = Array(1, 21, 11, 23, 15, 7, 41, 92, 36, 28, 35, 24, 6, 9, 15, 3, 25, 13, 17, 45)
MergeSort a, 0, 19
For i = 0 To UBound(a)
Debug.Print a(i)
Next
End Sub
Function MergeSort(Arr, ArrMin, ArrMax)
If ArrMax > ArrMin Then
ArrMid = Int((ArrMin + ArrMax) / 2)
MergeSort Arr, ArrMin, ArrMid
MergeSort Arr, ArrMid + 1, ArrMax
Merge Arr, ArrMin, ArrMid, ArrMax
End If
End Function
Function Merge(Arr, ArrMin, ArrMid, ArrMax)
Dim Temp()
ArrLen = ArrMax - ArrMin + 1
i = ArrMin
j = ArrMid + 1
k = ArrMin
ReDim Temp(ArrMin To ArrMax)
While i <= ArrMid And j <= ArrMax
If Arr(i) <= Arr(j) Then
Temp(k) = Arr(i)
k = k + 1
i = i + 1
Else
Temp(k) = Arr(j)
k = k + 1
j = j + 1
End If
Wend
While i <= ArrMid
Temp(k) = Arr(i)
k = k + 1
i = i + 1
Wend
While j <= ArrMax
Temp(k) = Arr(j)
k = k + 1
j = j + 1
Wend
Arr = Temp
End Function
|
|