📄 二分查找.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 2325
ClientLeft = 60
ClientTop = 465
ClientWidth = 5970
LinkTopic = "Form1"
ScaleHeight = 2325
ScaleWidth = 5970
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command2
Caption = "二分查找"
Height = 375
Left = 4680
TabIndex = 3
Top = 960
Width = 975
End
Begin VB.CommandButton Command1
Caption = "生成数组"
Height = 375
Left = 4680
TabIndex = 2
Top = 240
Width = 975
End
Begin VB.TextBox Text2
Height = 375
Left = 360
TabIndex = 1
Top = 960
Width = 3855
End
Begin VB.TextBox Text1
Height = 375
Left = 360
TabIndex = 0
Top = 240
Width = 3855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Base 1
Dim search As Variant
Private Sub Command1_Click()
Dim element As Variant
search = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
For Each element In search
Text1.Text = Text1.Text & Str(element)
Next element
End Sub
Private Sub Command2_Click()
Dim left As Integer, right As Integer, mid As Integer
Dim find As Integer, flg As Boolean
find = InputBox("输入要查找的数")
left = 1 'left代表查找区间的左端,初值为1
right = UBound(search) 'right代表查找区间的右端,初值为数组的上界
flg = False 'flg代表是否在数组中查找到指定的数字,初值为否
Do While left <= right
mid = (right + left) / 2 '计算出区间中间元素的位置
'若中间值即为要查找的值,则改变flg的值,并退出循环
If search(mid) = find Then
flg = True
Exit Do
'若find>search(mid),则find可能在search(mid)和search(right)区间中
ElseIf find > search(mid) Then
left = mid + 1
'若find<search(mid),则find可能在search(left)和search(mid)区间中
Else
right = mid - 1
End If
Loop
If flg Then
Text2.Text = "要查找的数" & CStr(find) & "是search(" & CStr(mid) & ")"
Else
Text2.Text = "在数列中没有找到" & Str(find)
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -