本帖最后由 baofa2 于 2024-4-28 10:37 编辑
- Sub test0() '类似案例,若不行则要有附件,改一下加上单位数排序
- Dim ar, br() As Long
- Dim i As Long, j As Long, k As Long, p As Long
- ar = Range("A1").CurrentRegion
- p = UBound(ar)
- For i = UBound(ar) To 1 Step -1
- If ar(i, 1) = "品名" Then
- k = 2
- For j = 3 To UBound(ar, 2)
- If Len(ar(i, j)) Then
- k = k + 1
- ReDim Preserve br(1 To k)
- br(k) = Val(Mid(ar(i, j), 2))
- Else
- Exit For
- End If
- Next
- BubbleSort ar, i + 1, p, 1, k, 2
- QuickSort ar, br, i, p, 3, k
- p = i - 1
- End If
- Next
- Range("L1").Resize(UBound(ar), UBound(ar, 2)) = ar
- Beep
- End Sub
- Function BubbleSort(ar, t As Long, b As Long, l As Long, r As Long, k As Long)
- Dim i As Long, x As Long, y As Long, swap, Flag As Boolean
- For i = t To b - 1
- Flag = True
- For y = t To b + t - 1 - i
- If ar(y, k) > ar(y + 1, k) Then
- Flag = False
- For x = l To r
- swap = ar(y, x)
- ar(y, x) = ar(y + 1, x)
- ar(y + 1, x) = swap
- Next
- End If
- Next
- If Flag Then Exit For
- Next
- End Function
- Function QuickSort(ar, br, t As Long, b As Long, l As Long, r As Long)
- Dim i As Long, j As Long, x As Long, y As Long, pivot As Long, swap
- j = l
- x = r
- pivot = br((l + r) \ 2)
- While j <= x
- Do
- If br(j) < pivot Then j = j + 1 Else Exit Do
- Loop While j < r
- Do
- If pivot < br(x) Then x = x - 1 Else Exit Do
- Loop While x > l
- If j < x Then
- swap = br(j): br(j) = br(x): br(x) = swap
- For y = t To b
- swap = ar(y, j): ar(y, j) = ar(y, x): ar(y, x) = swap
- Next
- j = j + 1: x = x - 1
- Else
- If j = x Then j = j + 1: x = x - 1
- End If
- Wend
- If l < x Then QuickSort ar, br, t, b, l, x
- If j < r Then QuickSort ar, br, t, b, j, r
- End Function
复制代码
|