| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

```Private Sub Command1_Click()
Call qsort(sj, 1, max_num) '对数组使用二分法
End Sub

Sub qsort(px() As Double, ByVal kaishi As Long, ByVal jieshu As Long)
Dim temp As Double, j As Long, i As Long
i = kaishi: j = jieshu '将i、j作为指针，从两侧向中部移动
If kaishi < jieshu Then '控制是否进入循环
temp = px(kaishi) '将数组第一个值赋给temp，暂时充当对比量
While i < j
Do While i < j '指针j从右向左移动，当遇到比temp小的数时，将该值移动到指针i的位置，并使i向右移动一位
If px(j) <= temp Then px(i) = px(j): i = i + 1: Exit Do
j = j - 1
Loop
Do While i < j '指针i从左向右移动，当遇到比temp大的数时，将该值移动到指针j的位置，并使j向左移动一位
If px(i) > temp Then px(j) = px(i): j = j - 1: Exit Do
i = i + 1
Loop
Wend
px(i) = temp
Call qsort(px(), kaishi, i - 1) '递归二分法过程进行排序
Call qsort(px(), i + 1, jieshu)
Else
Exit Sub
End If
End Sub```

```Private Sub Command1_Click()
l = max_num: S = 1: ReDim Num(l)
For px_i = S To l
Num(px_i) = sj(px_i)
Next px_i
Call QUICK_SORT(S, l, Num)
End Sub

'快速排序程序===========================================
Private Sub Exchange(ByRef n1 As Double, ByRef n2 As Double)
Dim t As Double
t = n1: n1 = n2: n2 = t
End Sub
Private Function PARTITION(ByVal p As Long, ByVal r As Long, ByRef a() As Double) As Long
Dim X As Double, t As Long, i As Long, j As Long
Randomize
t = CLng((r - p) * Rnd + p)
Call Exchange(a(r), a(t))
X = a(r): i = p - 1
For j = p To r - 1
If a(j) <= X Then i = i + 1: Call Exchange(a(i), a(j))
Next j
Call Exchange(a(i + 1), a(r))
PARTITION = i + 1
End Function
Private Sub QUICK_SORT(ByVal p As Long, ByVal r As Long, ByRef a() As Double)
If p < r Then
Dim q As Long
q = PARTITION(p, r, a)
Call QUICK_SORT(p, q - 1, a)
Call QUICK_SORT(q + 1, r, a)
End If
End Sub
```

Option Explicit

Private Declare Sub qsort CDecl Lib "msvcrt" ( _
ByRef pFirst As Any, _
ByVal lNumber As Long, _
ByVal lSize As Long, _
ByVal pfnComparator As Long)

Sub Main()
Dim z() As Long
Dim i As Long
Dim s As String

ReDim z(200000)
Randomize
For i = 0 To UBound(z)
z(i) = Int(Rnd * 1000000)
Next

qsort z(0), UBound(z) + 1, LenB(z(0)), AddressOf Comparator

Debug.Print "20万排序计时：" & GetTickCount() - t1 & "毫秒"
For i = 1999991 To UBound(z)   '为节省时间，二十万个数据取最后10个数据看一下
Debug.Print z(i)
Next

End Sub

Private Function Comparator CDecl( _
ByRef a As Long, _
ByRef b As Long) As Long
Comparator = a - b
End Function

VBCdeclFixDll插件https://wwi.

[此贴子已经被作者于2023-2-19 12:44编辑过]

VBCDeclFix.rar (1019.76 KB)

[此贴子已经被作者于2023-2-20 14:19编辑过]

• 14
• 1/2页
• 1
• 2