Name: Anonymous 2007-05-03 4:18 ID:0u2GQLpA
Hay guys... Need some help with some programming in Visual Basic. I know, I know, VB is for newbs and tards... I don't make the courses my university offers, sorry. In any case, I've never been great with recursion, and this is one of those... I need to replace the iterative function BinarySearch with it's recursive equivalent. Code is as follows:
---------------------------------------------
Dim array1 As Integer() = New Integer(14) {}
' FrmBinarySearch initializes array1 to ascending values
' 0, 2, 4, 6, ..., 28 when first loaded
Private Sub FrmBinarySearch_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To array1.GetUpperBound(0)
array1(i) = 2 * i
Next
End Sub ' FrmBinarySearch_Load
' event handler for cmdFindKey button
Private Sub cmdFindKey_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdFindKey.Click
Dim searchKey As Integer = Convert.ToInt32(txtInput.Text)
lblDisplay.Text = ""
' perform binary search
Dim element As Integer = BinarySearch(array1, searchKey)
If element <> -1 Then
lblResultOutput.Text = "Found value in element " & element
Else
lblResultOutput.Text = "Value not found"
End If
End Sub ' cmdFindKey_Click
' performs binary search
Function BinarySearch(ByVal array As Integer(), _
ByVal key As Integer) As Integer
Dim low As Integer = 0 ' low index
Dim high As Integer = array.GetUpperBound(0) ' high index
Dim middle As Integer ' middle index
While low <= high
middle = (low + high) \ 2
' the following line displays part
' of the array being manipulated during
' each iteration of loop
BuildOutput(low, middle, high)
If key = array(middle) Then ' match
Return middle
ElseIf key < array(middle) Then ' search low end
high = middle - 1 ' of array
Else
low = middle + 1
End If
End While
Return -1 ' search key not found
End Function ' BinarySearch
Sub BuildOutput(ByVal low As Integer, _
ByVal middle As Integer, ByVal high As Integer)
Dim i As Integer
For i = 0 To array1.GetUpperBound(0)
If i < low OrElse i > high Then
lblDisplay.Text &= " "
ElseIf i = middle Then ' mark middle element in output
lblDisplay.Text &= String.Format("{0:D2}", _
array1(i)) & "* "
Else
lblDisplay.Text &= String.Format("{0:D2}", _
array1(i)) & " "
End If
Next i
lblDisplay.Text &= vbCrLf
End Sub ' BuildOutput
End Class ' FrmBinarySearch
---------------------------------------------
Dim array1 As Integer() = New Integer(14) {}
' FrmBinarySearch initializes array1 to ascending values
' 0, 2, 4, 6, ..., 28 when first loaded
Private Sub FrmBinarySearch_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To array1.GetUpperBound(0)
array1(i) = 2 * i
Next
End Sub ' FrmBinarySearch_Load
' event handler for cmdFindKey button
Private Sub cmdFindKey_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdFindKey.Click
Dim searchKey As Integer = Convert.ToInt32(txtInput.Text)
lblDisplay.Text = ""
' perform binary search
Dim element As Integer = BinarySearch(array1, searchKey)
If element <> -1 Then
lblResultOutput.Text = "Found value in element " & element
Else
lblResultOutput.Text = "Value not found"
End If
End Sub ' cmdFindKey_Click
' performs binary search
Function BinarySearch(ByVal array As Integer(), _
ByVal key As Integer) As Integer
Dim low As Integer = 0 ' low index
Dim high As Integer = array.GetUpperBound(0) ' high index
Dim middle As Integer ' middle index
While low <= high
middle = (low + high) \ 2
' the following line displays part
' of the array being manipulated during
' each iteration of loop
BuildOutput(low, middle, high)
If key = array(middle) Then ' match
Return middle
ElseIf key < array(middle) Then ' search low end
high = middle - 1 ' of array
Else
low = middle + 1
End If
End While
Return -1 ' search key not found
End Function ' BinarySearch
Sub BuildOutput(ByVal low As Integer, _
ByVal middle As Integer, ByVal high As Integer)
Dim i As Integer
For i = 0 To array1.GetUpperBound(0)
If i < low OrElse i > high Then
lblDisplay.Text &= " "
ElseIf i = middle Then ' mark middle element in output
lblDisplay.Text &= String.Format("{0:D2}", _
array1(i)) & "* "
Else
lblDisplay.Text &= String.Format("{0:D2}", _
array1(i)) & " "
End If
Next i
lblDisplay.Text &= vbCrLf
End Sub ' BuildOutput
End Class ' FrmBinarySearch