posts - 70,comments - 80,trackbacks - 0

题目:编程:二分法插入排序。先生成10个两位整数,然后利用二分法插入排序进行排序。
答:o_ef.JPG
源程序:
Option Base 1
Dim a(10) As Integer

Private Sub Command1_Click()
Dim b() As Integer, i As Integer, k As Integer, flg As Integer
Dim ub As Integer
ub = UBound(a)
ReDim b(1)
b(1) = a(1)
For i = 2 To ub
    k = UBound(b)
    flg = fun(a(i), b)
    k = k + 1
    ReDim Preserve b(k)
    Do While k > flg
      b(k) = b(k - 1)
      k = k - 1
    Loop
    b(flg) = a(i)
Next i
Text2.Text = ""
For i = 1 To 10
   Text2.Text = Text2.Text & b(i) & "  "
Next i
End Sub

Private Function fun(x As Integer, b() As Integer)
Dim i As Integer, j As Integer, k As Integer
Dim ub As Integer
ub = UBound(b)
i = 1
Do While i <= ub
k = (i + ub) / 2
If x >= b(k) Then
   i = k + 1
ElseIf x < b(k) Then
   ub = k - 1
End If
Loop
fun = i
End Function

Private Sub Command2_Click()
Dim i As Integer
Randomize
Text1.Text = ""
For i = 1 To 10
  a(i) = Int(Rnd * 90) + 10
  Text1.Text = Text1.Text & a(i) & "  "
Next i
End Sub

Private Sub Command3_Click()
Text1.Text = ""
Text2.Text = ""
Command1.SetFocus
End Sub

Private Sub Command4_Click()
End
End Sub

posted on 2006-10-07 14:11 木子李 阅读(788) 评论(0)  编辑 收藏 引用 网摘 所属分类: Visual Basic 课堂

只有注册用户登录后才能发表评论。