跪求利用随机函数产生3000000个随机整数,用堆排序方法进行排序并统计出时间的程序
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/05 13:53:08
![跪求利用随机函数产生3000000个随机整数,用堆排序方法进行排序并统计出时间的程序](/uploads/image/z/2093813-53-3.jpg?t=%E8%B7%AA%E6%B1%82%E5%88%A9%E7%94%A8%E9%9A%8F%E6%9C%BA%E5%87%BD%E6%95%B0%E4%BA%A7%E7%94%9F3000000%E4%B8%AA%E9%9A%8F%E6%9C%BA%E6%95%B4%E6%95%B0%2C%E7%94%A8%E5%A0%86%E6%8E%92%E5%BA%8F%E6%96%B9%E6%B3%95%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F%E5%B9%B6%E7%BB%9F%E8%AE%A1%E5%87%BA%E6%97%B6%E9%97%B4%E7%9A%84%E7%A8%8B%E5%BA%8F)
跪求利用随机函数产生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