作业帮 > 数学 > 作业

随机产生10个介于0到100之间的整数,分别使用选择法和冒泡法对其进行升序排序

来源:学生作业帮 编辑:大师作文网作业帮 分类:数学作业 时间:2024/11/10 19:23:28
随机产生10个介于0到100之间的整数,分别使用选择法和冒泡法对其进行升序排序
随机产生10个介于0到100之间的整数,分别使用选择法和冒泡法对其进行升序排序
选择法:
Private Sub Form_Click()
Dim i%,j%,a%(1 To 10)
Randomize
For i = 1 To 10
a(i) = Int(101 * Rnd)
Next i
For i = 1 To 9
k = i
For j = i + 1 To 10
If a(j) < a(k) Then k = j
Next j
If k i Then t = a(i):a(i) = a(k):a(k) = t
Next i
For i = 1 To 10
Print a(i);
Next
End Sub
冒泡法:
Private Sub Form_Click()
Dim i%,j%,a%(1 To 10)
Randomize
For i = 1 To 10
a(i) = Int(101 * Rnd)
Next i
For i = 1 To 10
For j = 1 To 10 - i
If a(j) > a(j + 1) Then
k = a(j):a(j) = a(j + 1):a(j + 1) = k
End If
Next j
Next i
For i = 1 To 10
Print a(i);
Next
End Su