Private Sub Command1_Click()
Dim c(100000) As Long
Dim b() As Long
Dim i As Long
For i = 0 To UBound(c)
c(i) = Fix(Rnd * 100000)
Next
longSort c, b
For i = 0 To UBound(b)
Print b(i)
Next
End Sub
'==============================================
'正整数快速排序法.
'适合正整数,速度极快,数的范围越大,耗费的内存越多
'对相同的元素进行剔除!!
Sub longSort(Ay() As Long, AySort() As Long)
'Ay():排序前数组
'AySort:排序后的数组
'==============================================
Dim max As Long
Dim min As Long
Dim bY() As Boolean
Dim i As Long
Dim count As Long
max = Ay(0)
min = Ay(0)
'找出最大,最小值
For i = 0 To UBound(Ay)
If Ay(i) > max Then max = Ay(i)
If Ay(i) < min Then min = Ay(i)
Next
'把存在的数的布尔值设成真
ReDim bY(max - min)
For i = 0 To UBound(Ay)
bY(Ay(i) - min) = True
Next
'排序
For i = 0 To max - min
If bY(i) = True Then
ReDim Preserve AySort(count)
AySort(count) = i + min
count = count + 1
End If
Next
End Sub