📄 对分查找.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "对分查找"
ClientHeight = 3945
ClientLeft = 60
ClientTop = 450
ClientWidth = 4335
LinkTopic = "Form1"
ScaleHeight = 3945
ScaleWidth = 4335
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "查找"
Height = 375
Left = 2760
TabIndex = 8
Top = 3360
Width = 975
End
Begin VB.TextBox Text3
BackColor = &H00FF0000&
Height = 375
Left = 2640
Locked = -1 'True
TabIndex = 7
Top = 2640
Width = 1335
End
Begin VB.TextBox Text2
BackColor = &H00FF0000&
Height = 375
Left = 2640
Locked = -1 'True
TabIndex = 5
Top = 1440
Width = 1335
End
Begin VB.TextBox Text1
BackColor = &H0000FFFF&
Height = 375
Left = 2640
TabIndex = 3
Top = 480
Width = 1335
End
Begin VB.ListBox List1
BackColor = &H00FF0000&
Height = 3300
ItemData = "对分查找.frx":0000
Left = 120
List = "对分查找.frx":0002
TabIndex = 0
Top = 480
Width = 2175
End
Begin VB.Label Label4
Caption = "查找的次数"
Height = 375
Left = 2640
TabIndex = 6
Top = 2040
Width = 1335
End
Begin VB.Label Label3
Caption = "查找的结果"
Height = 255
Left = 2640
TabIndex = 4
Top = 960
Width = 1095
End
Begin VB.Label Label2
Caption = "输入查找键"
Height = 255
Left = 2640
TabIndex = 2
Top = 120
Width = 1215
End
Begin VB.Label Label1
Caption = "数组d中的数据"
Height = 255
Left = 240
TabIndex = 1
Top = 120
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim d(1 To 128) As Integer
Dim nc As Integer
Function adj(a As String, n As Integer) As String
Dim sa As String
sa = a: na = Len(a)
For i = 1 To n - na
sa = " " + sa
Next i
adj = sa
End Function
Function search(key As Integer) As Integer '对分查找算法
Dim pos, i, j As Integer
i = 1: j = 128: nc = 0
Do While i <= j
nc = nc + 1
m = Fix((i + j) / 2)
If d(m) = key Then
search = m: Exit Function '找到,返回在数组中的序号
End If
If key < d(m) Then
j = m - 1
Else
i = m + 1
End If
Loop
search = 0 '返回结果,0表示未找到
End Function
Private Sub Command1_Click()
Dim k, pos As Integer
pos = search(Val(Text1.Text)) '进行查找
If pos = 0 Then Text2.Text = "找不到" Else Text2.Text = "在d(" + Str(pos) + ")中"
Text3.Text = nc
nc = 0
End Sub
Private Sub Form_Load()
Dim dv As Integer
dv = 2
For i = 1 To 128
dv = dv + Fix((Timer() Mod 67 + 13) * Rnd) + 1 '随机产生递增序列的整数
d(i) = dv
List1.AddItem adj(Str(i), 3) + ":" + adj(Str(d(i)), 6)
Next i
End Sub
Private Sub Text1_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -