组卷题库 > 高中信息技术试卷库
试题详情
小吴为了研究冒泡排序过程中数据的“移动”情况,编写了一个VB程序,功能如下:在列表框list1中显示排序前数据(存储在数组a中),在文本框text1中输入初始位置(即下标值),单击“排序”按钮command1后,在标签label1中显示指定初始位置的数据在排序过程中的位置变化情况,排序后的数据显示在列表框list2中。程序运行界面如图所示。

实现上述功能的VB程序如下。

Dim a(1 To 8) As Integer

Dim n As Integer

Private Sub Form_Load()

a(1) = 30: a(2) = 47: a(3) = 30: a(4) = 72

a(5) = 70: a(6) = 23: a(7) = 99: a(8) = 24

n = 8

For i = 1 To 8

List1.AddItem str(a(i))

Next i

End Sub

Private Sub Command1_Click()

Dim i As Integer, j As Integer, k As Integer

Dim pos As Integer

Dim s As String

s = Text1.Text

pos = Val(Text1.Text)

For i = 1 To n - 1

For j = n To i + 1 Step -1

If a(j) < a(j - 1) Then

                       '改错

a(j - 1) = a(j)

a(j) = k      

'如果pos位置的数据参与交换,则更新pos值,记录pos变化位置

If pos = j Then

pos = j - 1

s = s + "→" + Str(pos)

        elseif       ②          then

pos = j

s = s + "→" + Str(pos)

End If

End If

Next j

Next i

Label1.Caption = "位置变化情况:" + s

List2.Clear

For i = 1 To n

List2.AddItem Str(a(i))

Next i

End Sub

知识点
参考答案
采纳过本试题的试卷
教育网站链接