a(1) |
a(2) |
a(3) |
…… |
a(n-2) |
a(n-1) |
a(n) |
27 |
35 |
39 |
…… |
49 |
39 |
33 |
小華由此設(shè)計一個VB程序,功能如下:單擊“排序”按鈕Command1,隨機(jī)生成10個兩位奇數(shù),將隨機(jī)生成的數(shù)據(jù)序列顯示在文本框Text1中,在文本框Text2中顯示完成排序后的數(shù)據(jù)序列,運(yùn)行結(jié)果如圖所示。
Private Sub Command1_Click()
Dim a(1 To 10) As Integer
Randomize
Const n = 10
For i = 1 To n
a(i) = ①
Text1.Text = Text1.Text + Str(a(i))
Next i
For i = 1 To n \ 2
For j = n - i + 1 To i + 1 Step -1
If a(j) < a(j - 1) Then
t = a(j - 1)
②
a(j) = t
End If
Next j
For j = i + 1 To n - i
If Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
For i = 1 To n
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub