跪求利用随机函数产生3000000个随机整数,用堆排序方法进行排序并统计出时间的程序
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 06:37:57
跪求利用随机函数产生3000000个随机整数,用堆排序方法进行排序并统计出时间的程序
跪求利用随机函数产生3000000个随机整数,用堆排序方法进行排序并统计出时间的程序
跪求利用随机函数产生3000000个随机整数,用堆排序方法进行排序并统计出时间的程序
'我的笔记本要花费23秒排序
Private Sub Command1_Click()
Dim a(3000000 - 1) As Integer, i As Long
Dim t0 As Date, t1 As Date
Randomize
For i = 0 To 3000000 - 1
a(i) = Int(Rnd * 1000)
Next
t0 = Now
Call Heap(a)
t1 = Now
MsgBox "排序3000000个随机数花费" & DateDiff("s", t0, t1) & "秒"
End Sub
Sub Heap(MyArray() As Integer)
Dim Index As Long
Dim Size As Long
Dim TEMP As Integer
Size = UBound(MyArray)
Index = 1
While (Index 0)
TEMP = MyArray(0)
MyArray(0) = MyArray(Index)
MyArray(Index) = TEMP
Call HeapSiftdown(MyArray(), Index - 1)
Index = Index - 1
gIterations = gIterations + 1
Wend
End Sub
Sub HeapSiftdown(MyArray() As Integer, M As Long)
Dim Index As Long
Dim Parent As Long
Dim TEMP As Integer
Index = 0
Parent = 2 * Index
Do While (Parent = MyArray(Parent) Then
Exit Do
End If
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP
Index = Parent
Parent = 2 * Index
gIterations = gIterations + 1
Loop
End Sub
Sub HeapSiftup(MyArray() As Integer, M As Long)
Dim Index As Long
Dim Parent As Long
Dim TEMP As Integer
Index = M
Do While (Index > 0)
Parent = Int(Index / 2)
If MyArray(Parent) >= MyArray(Index) Then
Exit Do
End If
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP
Index = Parent
gIterations = gIterations + 1
Loop
End Sub