Results 1 to 10 of 10

Thread: [RESOLVED] Binary Search of Arrays/Lists

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Resolved [RESOLVED] Binary Search of Arrays/Lists

    my binary search isnt working, i think it has to do with the fact that im using a list and its searching thru an array idk could you guys please help me out

    List Code:
    Code:
    Option Strict On
    
    Public Class Form1
    
        Dim J As Integer = 0
        Dim MyArray As New List(Of Integer)
    
    
    Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click
            Dim number As Integer
            Integer.TryParse(txtNumbers.Text, number)
            If number < 1 Then
                MessageBox.Show("Please enter a number greater then zero.")
            Else
                MyArray.Add(number)
                txtNumbers.Clear()
    
            End If
        End Sub
    
    Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click
            
    
            txtDisplayNumbers.Clear()
            txtDisplayNumbers.Focus()
    
            For J As Integer = 0 To MyArray.Count - 1
                txtDisplayNumbers.Text &= MyArray(J).ToString &                      
                 Environment.NewLine
            Next
        End Sub

    Binary Search :
    Code:
    Private Sub btnBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinarySearch.Click
    
            Dim Key As Integer
            Dim Low As Integer
            Dim High As Integer
            Dim Mid As Integer
    
    
    
    
            txtBinaryKey.Text = CStr(Key)
            Low = 0
            High = 4
    
            While Low <= High
                Mid = (High + Low) \ 2
                If Key = MyArray(Mid) Then
                    lblBinarySearchLocation.BackColor = Color.Silver
                    lblBinarySearch.Text = "Search Key found"
                    lblBinarySearchLocation.Text = "Array(" & Str(Mid) & ")"
    
                    Low = High + 1
    
                ElseIf Key < MyArray(Mid) Then
                    High = Mid - 1
                ElseIf Key > MyArray(Mid) Then
                    Low = Mid + 1
                End If
            End While
    
            If Key <> MyArray(Mid) Then
                lblBinarySearch.Text = "Search Key not found."
                lblBinarySearchLocation.BackColor = Color.Red
                lblBinarySearchLocation.Text = ""
            End If
    
        End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Binary Search of Arrays/Lists

    There appear to be a few issues right off the top:

    Code:
            txtBinaryKey.Text = CStr(Key)
            Low = 0
            High = 4
    Since you never put a value into key, all this line does would be to put 0 into the textbox. That surely isn't what you are trying to do. The key is what you are searching for isn't it? Is it supposed to have been entered into the textbox by the user? If so, then you have that first line backwards, because you are putting 0 into the textbox rather than putting the textbox contents into Key. For that, you could use CInt rather than CStr, but CInt will throw an exception if the user doesn't enter a valid integer into the textbox, so it is safer to use Integer.TryParse.

    As for the next two lines, I would agree that Low is 0, as that would be the bottom of the list, but setting High = 4 is going to be pretty bad. That would guarantee that the search will only find a match in the first five items of the collection. Perhaps you will only have five items in the collection, but that's still not a good reason to write it that way. Instead, set High = MyArray.Count-1.

    Finally, I'd suggest coming up with a different name for Mid. It should work in your code, but Mid is also a legacy method, so the compiler might confuse the variable for the function in some cases and produce weird behavior.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Binary Search of Arrays/Lists

    Quote Originally Posted by Shaggy Hiker View Post
    There appear to be a few issues right off the top:

    Code:
            txtBinaryKey.Text = CStr(Key)
            Low = 0
            High = 4
    Since you never put a value into key, all this line does would be to put 0 into the textbox. That surely isn't what you are trying to do. The key is what you are searching for isn't it? Is it supposed to have been entered into the textbox by the user? If so, then you have that first line backwards, because you are putting 0 into the textbox rather than putting the textbox contents into Key. For that, you could use CInt rather than CStr, but CInt will throw an exception if the user doesn't enter a valid integer into the textbox, so it is safer to use Integer.TryParse.

    As for the next two lines, I would agree that Low is 0, as that would be the bottom of the list, but setting High = 4 is going to be pretty bad. That would guarantee that the search will only find a match in the first five items of the collection. Perhaps you will only have five items in the collection, but that's still not a good reason to write it that way. Instead, set High = MyArray.Count-1.

    Finally, I'd suggest coming up with a different name for Mid. It should work in your code, but Mid is also a legacy method, so the compiler might confuse the variable for the function in some cases and produce weird behavior.
    thank you for your help
    so i changed what you said but i still get an error for some reason
    and i had the
    Code:
    txtbinary.text = cstr(Key)
    because i get an error if id it like
    Code:
     Key = Cint(txtBinary.Text)
    but i changed the code arround to use integer.Parse

    Code:
     Private Sub btnBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinarySearch.Click
    
            Dim Key As Integer
            Dim Low As Integer
            Dim High As Integer
            Dim Mid1 As Integer
    
    
    
    
            Key = Integer.Parse(txtBinaryKey.Text)
            Low = 0
            High = MyArray.Count - 1
    
            While Low <= High
                Mid1 = (High + Low) \ 2
                If Key = MyArray(Mid1) Then
                    lblBinarySearchLocation.BackColor = Color.Silver
                    lblBinarySearch.Text = "Search Key found"
                    lblBinarySearchLocation.Text = "Array(" & Str(Mid1) & ")"
    
                    Low = High + 1
    
                ElseIf Key < MyArray(Mid1) Then
                    High = Mid1 - 1
                ElseIf Key > MyArray(Mid1) Then
                    Low = Mid1 + 1
                End If
            End While
    
            If Key <> MyArray(Mid1) Then
                lblBinarySearch.Text = "Search Key not found."
                lblBinarySearchLocation.BackColor = Color.Red
                lblBinarySearchLocation.Text = ""
            End If
    
        End Sub
    I still a get an error:
    FormatExcpetion was unhandled.
    Input string was not in a correct format.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Binary Search of Arrays/Lists

    Integer.Parse is not the same as Integer.TryParse, which is what I recommended. It is used differently, too:
    Code:
    If Integer.TryParse(txtBinary.Text, Key) Then
     'It was an integer, and is now in Key.
    Else
     'It was not an integer, so do whatever you want here.
    End If
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Binary Search of Arrays/Lists

    Thank you for your help and patience, but any number i enter it wont even go thru the first if statement, its saying that the any/all numbers i enter are not integers ?

    Code:
     
    Private Sub btnBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinarySearch.Click
    
            Dim Key As Integer
            Dim Low As Integer
            Dim High As Integer
            Dim Mid1 As Integer
    
            Low = 0
            High = MyArray.Count - 1
    
            If Integer.TryParse(txtBinaryKey.Text, Key) Then
                While Low <= High
                    Mid1 = (High + Low) \ 2
                    If Key = MyArray(Mid1) Then
                        lblBinarySearchLocation.BackColor = Color.Silver
                        lblBinarySearch.Text = "Search Key found"
                        lblBinarySearchLocation.Text = "Array(" & Str(Mid1) & ")"
    
                        Low = High + 1
    
                    ElseIf Key < MyArray(Mid1) Then
                        High = Mid1 - 1
                    ElseIf Key > MyArray(Mid1) Then
                        Low = Mid1 + 1
                    End If
                End While
    
                If Key <> MyArray(Mid1) Then
                    lblBinarySearch.Text = "Search Key not found."
                    lblBinarySearchLocation.BackColor = Color.Red
                    lblBinarySearchLocation.Text = ""
                End If
    
            Else
                MessageBox.Show("Please enter a valid number.")
            End If
         
        End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Binary Search of Arrays/Lists

    Yea something is definitely wrong, i cant enter ANY number(negative, decimal, integer) its just to the end of the loop and gives me the message
    Code:
      
            Else
                MessageBox.Show("Please enter a valid number.")
            End If

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Binary Search of Arrays/Lists

    What are you entering? Is there something funny with your textbox? You aren't adding spaces, symbols, or something like that are you?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Binary Search of Arrays/Lists

    Im entering : 1,2,4,then i tired negative numbers -2,-4,-9. Im not adding any spaces either... i don't think there anything wrong with the textbox, ill re-make the textbox see if that changes things...

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    41

    Re: Binary Search of Arrays/Lists

    hmm that's really weird, i made a different textbox and now it works.
    once again THANK YOU FOR ALL YOUR HELP!

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [RESOLVED] Binary Search of Arrays/Lists

    That IS really weird. The only thing I can think of is that you had a space as default text in the original textbox. Then, when you went to add more text, there would be a space at the beginning of anything else you entered. You probably wouldn't notice a space, as it would be a thin and invisible character, but you might never have gotten rid of it, either, and it would mess up the TryParse. Just a guess, though.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width