Public Class Form1
Private Sub GenerateAndSearch(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click, btnSearch.Click
Static intDataArray(-1) As Integer
Dim btnButtonClicked As Button = sender
Select Case btnButtonClicked.Tag
Case "Generate Array"
Call GenerateSortedArray(intDataArray)
Me.lstListArrayElements.Items.Clear()
Call DisplayData(intDataArray, Me.lstListArrayElements, "Sorted array:")
Case "Search Array"
Dim intNumToFind As Integer = Val(Me.txtNumToFind.Text)
Dim intNumFoundIndex As Integer
intNumFoundIndex = BinarySearch(intDataArray, intNumToFind)
If intNumFoundIndex = -1 Then
Me.lblFoundMessage.Text = "Number not found."
Else
Me.lblFoundMessage.Text = "Number found at index" & intNumFoundIndex
End If
End Select
End Sub
Sub GenerateSortedArray(ByRef intArray() As Integer)
Const intNUMELEMENTS As Integer = 50
Const intMAXNUMBER As Integer = 100
ReDim intArray(intNumElements - 1)
Randomize()
Dim intIndex As Integer
For intIndex = 0 To intArray.Length - 1
intArray(intIndex) = Int(intMAXNUMBER * Rnd()) + 1
Next intIndex
Call InsertionSort(intArray)
End Sub
Sub InsertionSort(ByRef intArray() As Integer)
Dim intIndex, intPreviousIndex, intTempItem As Integer
For intIndex = 1 To intArray.Length - 1
intTempItem = intArray(intIndex)
intPreviousIndex = intIndex - 1
Do While intPreviousIndex > 0 And
intArray(intPreviousIndex) > intTempItem
intArray(intPreviousIndex + 1) = intArray(intPreviousIndex)
intPreviousIndex = intPreviousIndex - 1
Loop
If intArray(intPreviousIndex) > intTempItem Then
intArray(intPreviousIndex + 1) = intArray(intPreviousIndex)
intArray(intPreviousIndex) = intTempItem
Else
intArray(intPreviousIndex + 1) = intTempItem
End If
Next intIndex
End Sub
Sub DisplayData(ByRef intArray() As Integer, ByRef lstList As ListBox, ByVal strTitle As String)
lstList.Items.Add(strTitle)
Dim intIndex As Integer
For intIndex = 0 To intArray.Length - 1
lstList.Items.Add(intIndex & vbTab & intArray(intIndex))
Next intIndex
End Sub
Function BinarySearch(ByRef intArray() As Integer, ByVal intNumToFind As Integer) As Integer
Dim intHighIndex As Integer = intArray.Length - 1
Dim intMidIndex As Integer
Dim intLowIndex As Integer = 0
Dim blnFound As Boolean = False
Do While (Not blnFound) And (intLowIndex <= intHighIndex)
intMidIndex = (intMidIndex + intLowIndex) / 2
If intArray(intMidIndex) = intNumToFind Then
blnFound = True
ElseIf intArray(intMidIndex) > intNumToFind Then
intHighIndex = intMidIndex - 1
Else
intLowIndex = intMidIndex + 1
End If
Loop
If blnFound Then
Return intMidIndex
Else
Return -1
End If
End Function
Private Sub NewData_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumtoFind.TextChanged, btnGenerate.Click
Me.lblFoundMessage.text = Nothing
End Sub
End Class
我的问题是我无法让我的代码显示要查找的数组列表。我不知道我哪里出错任何想法?此外,我的排序按钮不起作用,但可能是因为当我点击按钮btnGenerate
时没有显示任何内容。
答案 0 :(得分:0)
NewData_TextChanged方法会删除搜索结果:
Me.lblFoundMessage.text = Nothing
删除该行代码并显示结果。
答案 1 :(得分:0)
您最大的问题似乎出现在BinarySearch功能中。你没有得到任何显示的原因是BinarySearch函数有一个无限循环。这是一些带有解释的更正代码,经过测试和运行:
Function BinarySearch(ByRef intArray() As Integer, ByVal intNumToFind As Integer) As Integer
Dim intHighIndex As Integer = intArray.Length - 1
Dim intMidIndex As Integer
Dim intLowIndex As Integer = 0
Dim blnFound As Boolean = False
Do While (Not blnFound) And (intLowIndex <= intHighIndex)
'This should add high and low then divide by 2 not mid and low
'Also you should use the integer division sign to return an integer result
intMidIndex = (intHighIndex + intLowIndex) \ 2
If intArray(intMidIndex) = intNumToFind Then
blnFound = True
'mid should become the new limit without any inc/decrement
ElseIf intArray(intMidIndex) > intNumToFind Then
intHighIndex = intMidIndex
Else
intLowIndex = intMidIndex
End If
Loop
If blnFound Then
Return intMidIndex
Else
Return -1
End If
End Function
应该做出的其他一些更正:
Select Case btnButtonClicked.Tag
应为Select Case btnButtonClicked.Tag.ToString()
intArray(intIndex) = Int(intMAXNUMBER * Rnd()) + 1
应为intArray(intIndex) = CInt(intMAXNUMBER * Rnd()) + 1
另外一个想法,您可能想要检查生成例程以仅生成唯一数字。二进制搜索只会找到一个元素,但如果有更多元素,则不会找到任何其他元素。