本帖最后由 lee1892 于 2013-1-22 21:59 编辑
香川群子 发表于 2013-1-22 12:24
直接比对的算法代码,没你想象的那么简单。
要不你写个代码试一试? 肯定不如我的方法好。
这不是好不好的问题吧,你那个办法是偷换概念嘛~
对比,也就是一个循环嵌套的事,又不麻烦。倒是排序啰嗦点儿~
写了个Merge的,没仔细调试,看个意思~
代码: | Function Merge(aRange1, aRange2)
Dim nLB1&, nUB1&, nLB2&, nUB2&
Dim aRes#(), i&
nLB1 = LBound(aRange1): nUB1 = UBound(aRange1)
nLB2 = LBound(aRange2): nUB2 = UBound(aRange2)
ReDim aRes(1 To nUB1 - nLB1 + nUB2 - nLB2 + 2, 1 To 2)
ReDim aTemp(1 To nUB1 - nLB1 + nUB2 - nLB2 + 2, 1 To 2)
For i = nLB1 To nUB1
aRes(i, 1) = aRange1(i, 1): aRes(i, 2) = aRange1(i, 2)
Next
For i = nLB2 To nUB2
aRes(i - nLB2 + 2 + nUB1 - nLB1, 1) = aRange2(i, 1)
aRes(i - nLB2 + 2 + nUB1 - nLB1, 2) = aRange2(i, 2)
Next
Call Unify(aRes)
Merge = aRes
End Function
Private Sub Unify(aRange)
Dim nLB&, nUB&, i&, j&, aTemp#(), nCount&
Call Sort(aRange)
nLB = LBound(aRange): nUB = UBound(aRange)
ReDim aTemp(1 To nUB - nLB + 1, 1 To 2)
For i = 1 To nUB - nLB + 1
nCount = nCount + 1
aTemp(nCount, 1) = aRange(i, 1)
aTemp(nCount, 2) = aRange(i, 2)
For j = i + 1 To nUB - nLB + 1
If aRange(j, 1) > aTemp(nCount, 2) Then Exit For
If aRange(j, 2) > aTemp(nCount, 2) Then
aTemp(nCount, 2) = aRange(j, 2)
End If
Next
If j > i + 1 Then i = j - 1
Next
Erase aRange
ReDim aRange(1 To nCount, 1 To 2)
For i = 1 To nCount
aRange(i, 1) = aTemp(i, 1): aRange(i, 2) = aTemp(i, 2)
Next
End Sub
Private Sub Sort(aRange)
Dim nLB&, nUB&, i&, j&, dSwap#
nLB = LBound(aRange): nUB = UBound(aRange)
For i = nLB To nUB
If aRange(i, 1) > aRange(i, 2) Then
dSwap = aRange(i, 1): aRange(i, 2) = aRange(i, 1): aRange(i, 1) = dSwap
End If
Next
Call QuickSort(aRange, nLB, nUB)
For i = nLB To nUB - 1
For j = i + 1 To nUB
If aRange(i, 2) <> aRange(j, 2) Then Exit For
Next
If j > i + 1 Then
Call QuickSort(aRange, i, j - 1, 2)
i = j - 1
End If
Next
End Sub
Private Sub QuickSort(aRange, nLeft&, nRight&, Optional nCol& = 1)
Dim i&, j&, nOther&, dKey#, dSwap#
nOther = IIf(nCol = 1, 2, 1)
If nLeft >= nRight Then Exit Sub
dKey = aRange(nLeft, nCol)
i = nLeft + 1: j = nRight
Do
Do While i <= nRight
If aRange(i, nCol) > dKey Then Exit Do
i = i + 1
Loop
Do While j > nLeft
If aRange(j, nCol) < dKey Then Exit Do
j = j - 1
Loop
If i >= j Then Exit Do
dSwap = aRange(i, nCol): aRange(i, nCol) = aRange(j, nCol): aRange(j, nCol) = dSwap
dSwap = aRange(i, nOther): aRange(i, nOther) = aRange(j, nOther): aRange(j, nOther) = dSwap
Loop
dSwap = aRange(nLeft, nCol): aRange(nLeft, nCol) = aRange(j, nCol): aRange(j, nCol) = dSwap
dSwap = aRange(nLeft, nOther): aRange(nLeft, nOther) = aRange(j, nOther): aRange(j, nOther) = dSwap
Call QuickSort(aRange, nLeft, j, nCol)
Call QuickSort(aRange, j + 1, nRight, nCol)
End Sub
|
|