为了账号安全,请及时绑定邮箱和手机立即绑定

你好,请问该如何利用随机函数生成30000个随机正整数,用多种方法进行排序?

你好,请问该如何利用随机函数生成30000个随机正整数,用多种方法进行排序?

至尊宝的传说 2022-03-19 15:11:06
利用随机函数生成30000个随机正整数,用多种方法进行排序。1.这些随机数的取值为5位整数,以每行15个显示。2.采用的排序方法不低于4种,取自:冒泡排序、直接插入排序、希尔排序、快速排序、选择排序、堆排序、归并排序等。3.分别计算每种算法排序所花费的时间,并简要对比分析。4.程序有菜单供选,有信息提示。
查看完整描述

1 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

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 <= Size)
Call HeapSiftup(MyArray, Index)
Index = Index + 1
gIterations = gIterations + 1
Wend

Index = Size
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 <= M)

If (Parent < M And MyArray(Parent) < MyArray(Parent + 1)) Then
Parent = Parent + 1
End If

If MyArray(Index) >= 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



查看完整回答
反对 回复 2022-03-22
  • 1 回答
  • 0 关注
  • 224 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信