小明编写了一个数据插入程序。程序输入一个数字,将该数字插入一列有序数据:-5,3,4,12,20,45,70,并使数据序列保持有序。设计的算法是从前往后找位置插入。
Private Sub Command1_Click()
Dim a(1 To 8) As Integer
Dim n, i, j, x As Integer
List1.Clear
List2.Clear
a(1) = -5: a(2) = 3: a(3) = 4: a(4) = 12: a(5) = 20: a(6) = 45: a(7) = 70
For j = 1 To 7
List1.AddItem Str(a(j))
Next j
x = Val(Text1.Text)
i = 1
Do While x > a(i) And i < 8 ’找x的插入位置
①
Loop
For j = 8 To i + 1 Step -1 ’移动数据
②
Next j
a(i) = x
For j = 1 To 8
List2.AddItem Str(a(j))
Next j
Label1.Caption = “插入的位置是” &Str(i)
End Sub