How do I display multiple results with binary search

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1070 Views - Last Post: 01 September 2011 - 03:18 AM Rate Topic: -----

#1 Fiendly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 11

How do I display multiple results with binary search

Posted 14 August 2011 - 04:54 PM

Having a lot of trouble with this project and I am currently stuck.
I want to produce a search that lets me have multiple search results but it only returns one.

How do I fix this?
My code is attached below, thanks for the help!

Public Class Form1
    Dim arrayAdoptions As Array
    '********************************************************************
    '*  Function name: Swap
    '*  Parameters: A (String), B (String)
    '*  Returns: Nil
    '*  Purpose: Swaps the memory location of items A and B
    '*********************************************************************
    Private Sub Swap(ByRef A As String, ByRef B As String)
        Dim T As String
        T = A
        A = B
        B = T
    End Sub
    '********************************************************************
    '*  Function name: QuickSort
    '*  Parameters: searchField (string), arraySort (string array)
    '*              First (integer), Last (integer)
    '*  Returns: Nil
    '*  Purpose: Sorts contents of 2D array arraySort acording to searchField
    '*********************************************************************
    Private Sub QuickSort(ByVal searchField As String, ByVal arraySort(,) As String, ByVal First As Integer, ByVal Last As Integer)
        Dim intCount As Integer = 0
        Dim intIndex = 0
        Dim intLow As Integer, intHigh As Integer
        Dim strMidValue As String

        Select Case searchField
            Case "Name"
                intIndex = 0
            Case "Breed"
                intIndex = 1
            Case "Age"
                intIndex = 2
            Case "Desexed"
                intIndex = 3
            Case "Size"
                intIndex = 4
            Case "Nature"
                intIndex = 5
            Case "Type"
                intIndex = 6
            Case "Trained"
                intIndex = 7
            Case "Hair Length"
                intIndex = 8
        End Select

        intLow = First
        intHigh = Last
        strMidValue = arraySort((First + Last) \ 2, intIndex).ToLower

        If intIndex <> 8 Then
            Do
                While arraySort(intLow, intIndex).ToLower < strMidValue
                    intLow = intLow + 1
                End While

                While arraySort(intHigh, intIndex).ToLower > strMidValue
                    intHigh = intHigh - 1
                End While

                If intLow <= intHigh Then
                    While intCount < 8
                        Swap(arraySort(intLow, intCount), arraySort(intHigh, intCount))
                        intCount = intCount + 1
                    End While
                    intLow = intLow + 1
                    intHigh = intHigh - 1
                End If
            Loop While intLow <= intHigh
            If First < intHigh Then QuickSort(searchField, arraySort, First, intHigh)
            If intLow < Last Then QuickSort(searchField, arraySort, intLow, Last)
        End If

    End Sub
    '********************************************************************
    '*  Function name: BinarySearch
    '*  Parameters: searchField (string), arraySort (string array)
    '*              strSearch (string)
    '*  Returns: intFound (integer) index location of found item, returns
    '*              -1 if nothing found
    '*  Purpose: Searches 2D array for any items matching strSearch
    '*********************************************************************
    Private Function binarySearch(ByVal searchField As String, ByVal searchArray(,) As String, ByVal strSearch As String)
        Dim intIndex = 0
        Dim intLower As Integer = 0
        Dim intUpper As Integer = (searchArray.Length / 9) - 1
        Dim intMiddle As Integer
        Dim intFound As Integer = -1

        Select Case searchField
            Case "Name"
                intIndex = 0
            Case "Breed"
                intIndex = 1
            Case "Age"
                intIndex = 2
            Case "Desexed"
                intIndex = 3
            Case "Size"
                intIndex = 4
            Case "Nature"
                intIndex = 5
            Case "Type"
                intIndex = 6
            Case "Trained"
                intIndex = 7
            Case "Hair Length"
                intIndex = 8
        End Select

        If intIndex <> 9 Then
            While intLower <= intUpper And intFound = -1
                intMiddle = (intLower + intUpper) / 2
                If searchArray(intMiddle, intIndex) < strSearch Then
                    intFound = intMiddle
                ElseIf searchArray(intMiddle, intIndex) > strSearch Then
                    intUpper = intMiddle - 1
                Else
                    intLower = intMiddle + 1
                End If
            End While
        End If

        Return intFound

    End Function

    '    Private Function binaryAge(ByVal searchField As String, ByVal searchArray(,) As String, ByVal strSearch As String)
    'Dim intLower As Integer = 0
    'Dim intUpper As Integer = (searchArray.Length / 9) - 1
    'Dim intMiddle As Integer
    'Dim intFound As Integer = -1
    '
    '       If cmbAge.Text = ">" Then 'If the cmbAge is equal to greate than'
    '          While intLower <= intUpper And intFound = -1
    '             intMiddle = (intLower + intUpper) / 2
    '            If searchArray(intMiddle, 2) > strSearch Then
    '               intFound = intMiddle
    '          ElseIf searchArray(intMiddle, 2) >= strSearch Then
    '             intUpper = intMiddle - 1
    '        Else
    '           intLower = intMiddle + 1
    '      End If
    ' End While
    'End If
    '
    '   If cmbAge.Text = "<" Then
    '        While intLower <= intUpper And intFound = -1
    '      intMiddle = (intLower + intUpper) / 2
    '       If searchArray(intMiddle, 2) < strSearch Then
    '            intFound = intMiddle
    '         ElseIf searchArray(intMiddle, 2) <= strSearch Then
    '              intUpper = intMiddle - 1
    '           Else
    '                intLower = intMiddle + 1
    '             End If
    '          End While
    '       End If
    '
    '       Return intFound
    '
    ' End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        arrayAdoptions = LoadTextFile("availableadoptions.txt")
        DisplayData(arrayAdoptions)
    End Sub
    '********************************************************************
    '*  Function name: LoadTextFile
    '*  Parameters: FileName(string)
    '*  Returns: array (array of strings) array containing data from text file
    '*  Purpose: Loads contents of text file in Filename into array
    '*********************************************************************
    Private Function LoadTextFile(ByVal Filename As String)
        Dim intCount As Integer = 0
        Dim intCount2 As Integer = 0
        Dim objRead As New System.IO.StreamReader(Filename)

        While objRead.Peek() <> -1
            intCount = intCount + 1
            objRead.ReadLine()
        End While

        Dim array(intCount / 9 - 1, 9 - 1) As String
        objRead.Close()
        Dim objRead2 As New System.IO.StreamReader(Filename)
        intCount = 0

        While objRead2.Peek() <> -1
            intCount2 = 0
            While intCount2 < 9
                array(intCount, intCount2) = objRead2.ReadLine()
                intCount2 = intCount2 + 1
            End While
            intCount = intCount + 1
        End While

        objRead2.Close()
        Return array

    End Function
    '********************************************************************
    '*  Function name: DisplayData
    '*  Parameters: arrayDisplay(array of strings)
    '*  Returns: Nil
    '*  Purpose: Displays contents of arrayDisplay to the ListView object
    '*              on form.
    '*********************************************************************
    Private Sub DisplayData(ByVal arrayDisplay(,))

        Dim row(9) As String
        Dim intCount As Integer = 0
        Dim intCount2 As Integer = 0
        Dim lvItem As New ListViewItem

        While intCount < (arrayDisplay.Length - 1) / 9
            intCount2 = 0
            While intCount2 < 9
                row(intCount2) = arrayDisplay(intCount, intCount2)
                intCount2 = intCount2 + 1
            End While
            lvPets.Items.Add(New ListViewItem(row))
            intCount = intCount + 1
        End While

    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Dim intFound As Integer = -1
        Dim intCount As Integer = 0

        If cmbSearch.Text = "" Then
            MsgBox("Please select a search field")
            Exit Sub
        End If

        QuickSort(cmbSearch.Text, arrayAdoptions, 0, (arrayAdoptions.Length / 9) - 1)
        intFound = binarySearch(cmbSearch.Text, arrayAdoptions, txtSearch.Text)
        If intFound = -1 Then
            MsgBox("No search results matching criteria")
            Exit Sub
        End If

        Dim arraySearchResults(0, 9)
        lvPets.Items.Clear()

        While intCount < 9
            arraySearchResults(0, intCount) = arrayAdoptions(intFound, intCount)
            intCount = intCount + 1
        End While

        DisplayData(arraySearchResults)

    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtSearch.Text = ""
        cmbAge.Text = ""
        lvPets.Items.Clear()
        DisplayData(arrayAdoptions)
    End Sub
End Class



*Posted wrong code*

This post has been edited by Fiendly: 14 August 2011 - 05:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I display multiple results with binary search

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How do I display multiple results with binary search

Posted 14 August 2011 - 05:10 PM

Your function returns an index of type integer. Instead you may return an array of integers that holds all indexes where a search matches. something like:
Dim intFound() As Integer
Dim i As Integer = 0
'then when found
If searchArray(intMiddle, intIndex) = strSearch Then
  intFound(i) = intMiddle
  i = i + 1 

Was This Post Helpful? 0
  • +
  • -

#3 Fiendly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 11

Re: How do I display multiple results with binary search

Posted 14 August 2011 - 05:16 PM

View Postsmohd, on 14 August 2011 - 05:10 PM, said:

Your function returns an index of type integer. Instead you may return an array of integers that holds all indexes where a search matches. something like:
Dim intFound() As Integer
Dim i As Integer = 0
'then when found
If searchArray(intMiddle, intIndex) = strSearch Then
  intFound(i) = intMiddle
  i = i + 1 


Any idea where this would go?
I'm a little confused by this.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How do I display multiple results with binary search

Posted 14 August 2011 - 05:22 PM

 Dim intFound() As Integer 'where you declared intFound
Dim i As Integer = 0/ 'ext empty line additional declaration
'then when found
If searchArray(intMiddle, intIndex) = strSearch Then 'you have this line
  intFound(i) = intMiddle ' at intFound = intMiddle
  i = i + 1 'below it 

Was This Post Helpful? 0
  • +
  • -

#5 Fiendly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 11

Re: How do I display multiple results with binary search

Posted 14 August 2011 - 05:32 PM

View Postsmohd, on 14 August 2011 - 05:22 PM, said:

 Dim intFound() As Integer 'where you declared intFound
Dim i As Integer = 0/ 'ext empty line additional declaration
'then when found
If searchArray(intMiddle, intIndex) = strSearch Then 'you have this line
  intFound(i) = intMiddle ' at intFound = intMiddle
  i = i + 1 'below it 


Thanks for this but I seem to be getting an error with
        If intIndex <> 9 Then
            While intLower <= intUpper And intFound = -1



With intFound = -1 returning an error stating that Operator = doesn't word for 1d types of arrays.
How would I go around by fixing this?
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How do I display multiple results with binary search

Posted 14 August 2011 - 08:06 PM

Now you have to change that if condition, and since you dont really need that second part, probably since you use And and not AndAlso, then just remove it from there:
While intLower <= intUpper

Was This Post Helpful? 1
  • +
  • -

#7 Fiendly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 11

Re: How do I display multiple results with binary search

Posted 15 August 2011 - 04:23 PM

View Postsmohd, on 14 August 2011 - 08:06 PM, said:

Now you have to change that if condition, and since you dont really need that second part, probably since you use And and not AndAlso, then just remove it from there:
While intLower <= intUpper


I fixed the problem!
Now I need to assign it to the button as it's still not displaying results..

This post has been edited by Fiendly: 15 August 2011 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 questionboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 11

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 03:45 AM

View PostFiendly, on 15 August 2011 - 04:23 PM, said:

View Postsmohd, on 14 August 2011 - 08:06 PM, said:

Now you have to change that if condition, and since you dont really need that second part, probably since you use And and not AndAlso, then just remove it from there:
While intLower <= intUpper


I fixed the problem!
Now I need to assign it to the button as it's still not displaying results..


How were you able to fix the problem, what did you change?
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 04:32 AM

View Postquestionboy123, on 29 August 2011 - 04:30 PM, said:

How were you able to fix the problem, what did you change?

Now intFound is an array, so it must be in array style.
Was This Post Helpful? 0
  • +
  • -

#10 Fiendly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 11

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 04:55 AM

View Postquestionboy123, on 29 August 2011 - 03:45 AM, said:

View PostFiendly, on 15 August 2011 - 04:23 PM, said:

View Postsmohd, on 14 August 2011 - 08:06 PM, said:

Now you have to change that if condition, and since you dont really need that second part, probably since you use And and not AndAlso, then just remove it from there:
While intLower <= intUpper


I fixed the problem!
Now I need to assign it to the button as it's still not displaying results..


How were you able to fix the problem, what did you change?


Sorry the problem that I fixed was changing intFound() As Integer = Nothing
However this didn't seem to solve the problem!
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1956
  • View blog
  • Posts: 8,700
  • Joined: 29-May 08

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 07:53 AM

Arrays are a reference type so doing
intFound() As Integer = Nothing

is "removing" the reference to the array.

It'll be a lot easier and simpler to return a List(Of Int) instead of an Array. When a search is a match add the index to the list.
Was This Post Helpful? 0
  • +
  • -

#12 Fiendly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-May 11

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 05:29 PM

View PostAdamSpeight2008, on 29 August 2011 - 07:53 AM, said:

Arrays are a reference type so doing
intFound() As Integer = Nothing

is "removing" the reference to the array.

It'll be a lot easier and simpler to return a List(Of Int) instead of an Array. When a search is a match add the index to the list.


Hi!
How would I actually implement this? The list is kind of new to me..
Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,685
  • Joined: 02-July 08

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 07:15 PM

'declaration
Private intFound As New List(Of Integer)

'in you loop where you search you add the value
intFound.Add(<integer found in search>)

'now you can do some cool stuff with your list.
intFound.Sort()
intFound.Max
intFound.Min
intFound.Sum
'etc...

Was This Post Helpful? 0
  • +
  • -

#14 questionboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 11

Re: How do I display multiple results with binary search

Posted 29 August 2011 - 08:39 PM

 Private Function binarySearch(ByVal searchField As String, ByVal searchArray(,) As String, ByVal strSearch As String)  

086         Dim intIndex = 0  

087         Dim intLower As Integer = 0  

088         Dim intUpper As Integer = (searchArray.Length / 9) - 1  

089         Dim intMiddle As Integer 

090         Dim intFound() As Integer
  
            Dim i As Integer = 0
091    

092         Select Case searchField  

093             Case "Name" 

094                 intIndex = 0  

095             Case "Breed" 

096                 intIndex = 1  

097             Case "Age" 

098                 intIndex = 2  

099             Case "Desexed" 

100                 intIndex = 3  

101             Case "Size" 

102                 intIndex = 4  

103             Case "Nature" 

104                 intIndex = 5  

105             Case "Type" 

106                 intIndex = 6  

107             Case "Trained" 

108                 intIndex = 7  

109             Case "Hair Length" 

110                 intIndex = 8  

111         End Select 

112    

113         If intIndex <> 9 Then 

114             While intLower <= intUpper  

115                 intMiddle = (intLower + intUpper) / 2  

116                 If searchArray(intMiddle, intIndex) < strSearch Then 

117                     intFound(i) = intMiddle  
                        i = i + 1
118                 ElseIf searchArray(intMiddle, intIndex) > strSearch Then 

119                     intUpper = intMiddle - 1  

120                 Else 

121                     intLower = intMiddle + 1  

122                 End If 

123             End While 

124         End If 

125    

126         Return intFound  

127    

128     End Function 




This is what i've got, but it still does work. The "intFound(i)= intMiddle" is highlighted and it says NullReference not set to an instance of an object. Tbh, i'd rather not do something new like List(Of Int) because i'm unsure of how it works. Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,685
  • Joined: 02-July 08

Re: How do I display multiple results with binary search

Posted 30 August 2011 - 03:23 AM

That's because you made an empty array - intFound. So you cannot add an element and give it value. If intFound was a List(Of Integer) you would change this line:

intFound(i) = intMiddle

with

intFound.Add(intMiddle)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2