|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
1.法师大侠,我拿您的代码出去交流,结果别人给我了一段很简单的快排代码。比您本帖提供的快了将近50%
我感觉是不是您所做的那些优化没意义了
2.本代码要怎样添加第二个参数,Optional SortUpDown as boolean = true- Private Sub QuickSort(ByRef key_arr() As Long, L As Long, R As Long)
- Dim i As Long, j As Long
- Dim x As Long, Swap As Long
- If R - L <= 16 Then
- For i = L To R
- x = i
- For j = i + 1 To R
- If key_arr(j) < key_arr(x) Then
- x = j
- End If
- Next j
-
- If x > i Then
- Swap = key_arr(i)
- key_arr(i) = key_arr(j)
- key_arr(j) = Swap
- End If
- Next i
- Else
- x = key_arr((L + R) \ 2)
- i = L
- j = R
- Do While i <= j
- Do While key_arr(i) < x
- i = i + 1
- Loop
-
- Do While key_arr(j) > x
- j = j - 1
- Loop
-
- If i <= j Then
- Swap = key_arr(i)
- key_arr(i) = key_arr(j)
- key_arr(j) = Swap
- i = i + 1
- j = j - 1
- End If
- Loop
- '递归方法
- If (L < j) Then Call QuickSort(key_arr, L, j)
- If (i < R) Then Call QuickSort(key_arr, i, R)
- End If
- End Sub
- Public Sub StartSort(ByRef vArray() As Long)
- Dim iLow As Long
- Dim iHi As Long
-
- '//get range of array
- iLow = LBound(vArray) '//Low bound
- iHi = UBound(vArray) '//High bound
-
- '//use STACK, not RECURSION
- QuickSort vArray(), iLow, iHi '//call the procedure
-
- End Sub
复制代码 |
|