数组a中存储n个2位正整数,从倒数第2个数开始,利用对分查找的思想,找到它的所在位置,并插入正确位置中,实现整个数组有序。实现该功能的VB程序如下:
Const n = 100
Dim a(n) As Integer
Private Sub Form_Load()
′产生n个2位正整数,并显示在文本框Text1中,代码略
End Sub
Private Sub Command1_Click( )
Dim i As
Integer, j As Integer, left As Integer
Dim right As
Integer, m As Integer, t As Integer
i = n-1
Do While i >
= 1
________
right = n
t = a(i)
Do While
left < = right
m =
Int((left + right)/2)
If a(m) = t Then right = m: Exit Do
If
a(m) < t Then
left
= m + 1
Else
right
= m-1
End If
Loop
For j = i
To right-1
a(j) =
a(j + 1)
Next j
________________
i = i-1
s = “”
For j = 1
To n
s = s
+ Str(a(j))
Next j
List1.AddItem
s
Loop
End Sub