Dim a(10)
Randomize
For i = 1 To 10
a(i) = Int(Rnd * i + 3 + Rnd * 10) '给数组赋值
Print a(i)
Next i
For i = 1 To 10 ‘
For j = i + 1 To 10
If a(j) <a(i) Then ’如果剩余的比a(i)小的话,就交换
t = a(j)
a(j) = a(i)
a(i) = t
End If
Next j
Next i
For k = 1 To 10
Print a(k) ‘打印排好的数
Next k
、
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。
它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。
Dim AA(1 To 10) As Integer, ZGCJ(1 To 10) As Integer, ZDCJ(1 To 10) As Integer在通用部分声明三个数组
Private Sub Command1_Click()Text1.Text = "": Text2.Text = "": Text3.Text = ""
Text1.Text = "系统自动生成的十个数:" & vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
Randomize
AA(I) = Int(Rnd * 90 + 10)
Text1.Text = Text1.Text & AA(I) & Space(4)
If I Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
End Sub
生成十个数的代码
Private Sub Command2_Click()Text2.Text = "": Text3.Text = ""
Dim AAA As Integer, BBB As Integer
For I = 1 To 9
For J = I + 1 To 10
If ZGCJ(I) < ZGCJ(J) Then
AAA = ZGCJ(I)
ZGCJ(I) = ZGCJ(J)
ZGCJ(J) = AAA
End If
If ZDCJ(J) < ZDCJ(I) Then
BBB = ZDCJ(J)
ZDCJ(J) = ZDCJ(I)
ZDCJ(I) = BBB
End If
Next J
Next I
Text2.Text = Text2.Text & "从大到小排列:" & vbCrLf
For I = 1 To 10
Text2.Text = Text2.Text & ZGCJ(I) & Space(4)
If I Mod 5 = 0 Then Text2.Text = Text2.Text & vbCrLf
Next I
Text3.Text = Text3.Text & "从小到大排列:" & vbCrLf
For I = 1 To 10
Text3.Text = Text3.Text & ZDCJ(I) & Space(4)
If I Mod 5 = 0 Then Text3.Text = Text3.Text & vbCrLf
Next I
End Sub
排序的代码。
如果需要自己输入数字,可以这样:
'如果要自己输入数字,可以修改下面的代码Text1.Text = "": Text2.Text = "": Text3.Text = ""
Text1.Text = "系统自动生成的十个数:" & vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
Randomize
AA(I) = Int(Rnd * 90 + 10)
Text1.Text = Text1.Text & AA(I) & Space(4)
If I Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
'----------------------------修改为:
Text1.Text = "": Text2.Text = "": Text3.Text = ""
Text1.Text = "用户输入的十个数:" & vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
AA(I) = Val(InputBox("请输入第" & I & "个数!"))
Text1.Text = Text1.Text & AA(I) & Space(4)
If I Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)