i am doing a recursive algorithm binary search project for school and i am unaware of what is causing the issue some help would be appreciated sorry if the format i am new to the forums
Code:
Option Explicit
Dim Words(1 To 100) As String
Dim UpBound As Integer
Dim LowBound As Integer
Dim Numrec As Integer
Dim Curmid As Integer
Dim Currec As Integer
Dim Counta As Integer
Dim FileNum As Integer
Dim Position As Integer
Dim SearchText As String
Dim ListCount As Integer
Private Sub Command1_Click()
SearchText = UCase(Text1.Text)
ListCount = -List1.ListCount
LowBound = LBound(Words)
UpBound = UBound(Words)
Position = BSearch(LowBound, UpBound, SearchText)
If Position = -1 Then
MsgBox "not found"
Text1.Text = ""
Text.Text.SetFocus
Else
MsgBox Text1.Text & "found in" & vbdlrf & "position: " & Position
List1.ListIndex = Position - 1
End If
End Sub
Function BSearch(Low As Integer, Upper As Integer, KeyField As String) As Integer
Dim Middle As Integer
Middle = Fix(Low + Upper) / 2
If Words(Middle) = KeyField Then
BSearch = Middle
Exit Function
End If
If Low >= Upper Then
BSearch = -1
Exit Function
End If
If KeyField < Words(Middle) Then
Upper = Middle - 1
Else
Low = Middle + 1
End If
BSearch = BSearch(Low, Upper, KeyField)
End Function
Private Sub Form_Load()
FileNum = FreeFile
Open App.Path & "\sorted.txt" For Input As FileNum
For Counta = 1 To 100
Input #FileNum, Words(Counta)
List1.AddItem Words(Counta)
Next Counta
MsgBox "words loaded"
Numrec = 100
LowBound = 0
UpBound = 100
Curmid = (UpBound - (LowBound - 1)) / 2
End Sub