SlideShare a Scribd company logo
1 of 8
Download to read offline
A Program in Binary Searching Using Visual Basic 2008 (version 9.0)
                           By Ariel T. Sumagaysay, MSCS

A. Form Design

Design the form shown below using the Visual Basic 2008:




B. Components

        Components                             Properties / Settings
    Form1                  Name: frmMain
                           Font: Times New Roman
                           Font Style: Regular
                           Size: 10
                           FormBorderStyle: FixedDialog
                           Icon: Any
                           MaximizeBox: False
                           StartPosition: CenterScreen
                           Text: BINARY SEARCH PLUS
    GroupBox1              Name: GroupBox1
                           Text:
    Label1                 Name: Label1
                           Text: ENTER NUMBER OF ELEMENTS [AT LEAST 10]:
    Label2                 Name: Label2
                           Text: ENTERED ELEMENTS
    Label3                 Name: Label3
                           Text: ARRANGED:
    Label4                 Name: Label4
                           Text: Results
2


B. Components (Continued…)

        Components                          Properties / Settings
   List1                Name: lstResults
                        Font: Times New Roman
                        Size: 13
   TextBox1             Name: txtNumberOfElements
   TextBox2             Name: txtEnterElements
   Textbox3             Name: txtAllElements
                        Font: Times New Roman
                        Font Style: Regular
                        Size: 14
                        Multiline: True
                        ReadOnly: True
                        ScrollBars: Both
                        WordWarp: False
   Textbox4             Name: txtArranged
                        Font: Times New Roman
                        Font Style: Regular
                        Size: 14
                        Multiline: True
                        ReadOnly: True
                        ScrollBars: Both
                        WordWarp: False
   Textbox5             Name: txtEnterSearch
                        Font: Times New Roman
                        Font Style: Regular
                        Size: 14
   Button1              Name: btnNumber
                        Text: &OK
   Button2              Name: btnEnterElement
                        Text: &ENTER ELEMENT
   Button3              Name: btnSearch
                        Image: any *.PNG related graphics
                        Text: &SEARCH
                        TextImageRelation: ImageAboveText
   Button4              Name: btnReset
                        Image: any *.PNG related graphics
                        Text: &RESET
                        TextImageRelation: ImageAboveText
   Button5              Name: btnQuit
                        Image: any *.PNG related graphics
                        Text: &QUIT
                        TextImageRelation: ImageAboveText
3


C. Code Listings

'BINARY SEARCH
'PROGRAM BY ARIEL T. SUMAGAYSAY
'COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY
'WEST NEGROS UNIVERSITY
'APRIL 04, 2010 1658 HR

'Limitations of the Program:The list must not have duplicate values

Option Explicit Off

Public Class frmMain

    'Variables
    Dim nNumberOfElements As Integer
    Dim nResetStatus As Integer
    Dim sNumbers As String
    Dim nFind As Integer
    Dim bfound As Boolean
    Dim nlocation As Integer
    Dim lowBound As Integer
    Dim HighBound As Integer
    Dim nDivTimes As Integer
    Dim nDTCtr As Integer
    Dim n As Integer
    Dim x As Integer
    Dim s As String

    'One Dimensional Array
    Dim aInputSet() As Integer

    Private Sub btnNumber_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNumber.Click
        'Get Number of Elements
        nNumberOfElements = Int(txtNumberOfElements.Text)

        If nNumberOfElements < 10 Then
            txtNumberOfElements.Enabled = False
            btnNumber.Enabled = False

               MsgBox("Number of Elements entered is less than 10")
               MsgBox("Click RESET button to enter number again")
               nResetStatus = 1
               btnReset.Enabled = True

        Else

               'Setting Buttons
               btnEnterElement.Enabled = True
               btnNumber.Enabled = False

               txtEnterElement.Enabled = True
               txtNumberOfElements.Enabled = False

               'Set the number of elements for the array
4

              n = nNumberOfElements
              ReDim aInputSet(nNumberOfElements)

            'set command button label
            s = Format(1, "0#")
            btnEnterElement.Text = "&ENTER ELEMENT " + s
        End If

        'Compute Number of time in Division by 2
        nDivTimes = System.Math.Truncate(System.Math.Sqrt(nNumberOfElements))

    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        'Initialize Variables
        nNumberOfElements = 0
        nResetStatus = 0
        nDivTimes = 0
        nDTCtr = 0

        'Initial Settings
        txtNumberOfElements.Text = " "
        txtAllElements.Text = " "
        txtArranged.Text = " "
        txtEnterElement.Text = " "
        txtEnterSearch.Text = "0"
        bfound = False
        lstResults.Items.Clear()

        'Setting Buttons - Controls
        btnEnterElement.Enabled = False
        btnSearch.Enabled = False
        btnReset.Enabled = False

        txtNumberOfElements.Enabled = True
        txtEnterElement.Enabled = False
        txtEnterSearch.Enabled = False

        'Initialize variables
        nNumberOfElements = 0
        n = 0
        x = 0
        s = " "
        sNumbers = " "        'Set string of numbers to NULL
        nFind = 0

        'Set the Number of Elements
        ReDim aInputSet(0)

    End Sub
5

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnReset.Click

        'For Enter less than 10 elements
        If nResetStatus = 1 Then
            txtNumberOfElements.Text = 0
            txtNumberOfElements.Enabled = True
            btnNumber.Enabled = True
        End If

        'For Resetting the values
        If nResetStatus = 2 Then
            'Initialize Variables
            nNumberOfElements = 0
            nResetStatus = 0
            nDivTimes = 0
            nDTCtr = 0

              'Initial Settings
              txtNumberOfElements.Text = " "
              txtAllElements.Text = " "
              txtArranged.Text = " "
              txtEnterElement.Text = " "
              txtEnterSearch.Text = "0"
              bfound = False
              lstResults.Items.Clear()

              'Setting Buttons - Controls
              btnEnterElement.Enabled = False
              btnSearch.Enabled = False
              btnReset.Enabled = False
              btnNumber.Enabled = True

              txtNumberOfElements.Enabled = True
              txtEnterElement.Enabled = False
              txtEnterSearch.Enabled = False

              'Initialize variables
              nNumberOfElements = 0
              n = 0
              x = 0
              s = " "
              sNumbers = " "        'Set string of numbers to NULL
              nFind = 0

            'Set the Number of Elements
            ReDim aInputSet(0)
        End If

    End Sub
6

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

       'Get the mid point
       y = nNumberOfElements
       mid = y / 2

        'Search Border Values
        Call BorderValues(1, y, mid)

        'Call the Binary Search Procedure
        'binarySearch(First, Last, Mid)
        If bfound = False Then
            Call binarySearch(1, y, mid)
        End If

        'Display Results
        Call DisplayResults()

       'Set Reset Status
       nResetStatus = 2

        btnSearch.Enabled = False
    End Sub

    Private Sub btnEnterElement_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnEnterElement.Click
        Static y As Integer
        y = 0

        'Enter numbers
        If txtEnterElement.Text <> " " Then
            x = x + 1
            y = x
            s = Format(y + 1, "0#")
            btnEnterElement.Text = "&ENTER ELEMENT " + s
            aInputSet(x) = CInt(txtEnterElement.Text)

            If x = n Then
                btnEnterElement.Enabled = False
                txtEnterElement.Enabled = False
                btnEnterElement.Text = "&ENTER ELEMENT"
                Call bubblesort()
            End If

            sNumbers = sNumbers & txtEnterElement.Text & " "
            txtAllElements.Text = sNumbers
            txtEnterElement.Text = " "
        End If

       'Set Reset Status
       nResetStatus = 2
       btnReset.Enabled = True

        'Enable Search Features
        txtEnterSearch.Enabled = True
    End Sub
7

    Private Sub bubblesort()
        'Sort the elements using bubblesort
        Static a As Integer
        Static b As Integer
        Static temp As Integer
        Static sHereNumbers As String

        'initialize variables
        a = 0
        b = 0
        temp = 0
        sHereNumbers = " "

        For a = 1 To n
             For b = 1 To n - 1
                  If (aInputSet(b) > aInputSet(b + 1)) Then
                      temp = aInputSet(b)
                      aInputSet(b) = aInputSet(b + 1)
                      aInputSet(b + 1) = temp
                  End If
             Next
        Next

        'display list
        For a = 1 To n
             sHereNumbers = sHereNumbers + Str(aInputSet(a)) & " "
             txtArranged.Text = sHereNumbers
        Next

    End Sub

    Private Sub txtEnterSearch_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtEnterSearch.TextChanged
        'Get value to search
        nFind = Int(txtEnterSearch.Text)

        'Enabled Search Button
        btnSearch.Enabled = True
    End Sub

    Private Sub binarySearch(ByVal First As Integer, ByVal Last As Integer,
ByVal Mid As Integer)

        'Counter
        nDTCtr = nDTCtr + 1

        If nDTCtr > nDivTimes Then
            Exit Sub
        End If

        'Binary Search Procedure
        If nFind > aInputSet(Mid) Then
            lowBound = Mid
            HighBound = Last
            lstResults.Items.Add(Str(nFind) & " is possibly located between
location " & Str(lowBound) & " and " & Str(HighBound))
8

            lstResults.Items.Add(Str(nFind) & " is possibly located between
values   " & aInputSet(lowBound) & " and " & aInputSet(HighBound))
            lstResults.Items.Add(" ")
            Mid = (lowBound + HighBound) / 2
            Call binarySearch(lowBound, HighBound, Mid)
        ElseIf nFind < aInputSet(Mid) Then
            lowBound = First
            HighBound = Mid
            lstResults.Items.Add(Str(nFind) & " is possibly located between
location " & Str(lowBound) & " and " & Str(HighBound))
            lstResults.Items.Add(Str(nFind) & " is possibly located between
values   " & aInputSet(lowBound) & " and " & aInputSet(HighBound))
            lstResults.Items.Add(" ")
            Mid = (lowBound + HighBound) / 2
            Call binarySearch(lowBound, HighBound, Mid)
        ElseIf nFind = aInputSet(Mid) Then
            nlocation = Mid
            bfound = True
        End If

    End Sub
    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnQuit.Click
        'Terminate Program
        End
    End Sub

    Private Sub BorderValues(ByVal First As Integer, ByVal Last As Integer,
ByVal Mid As Integer)
        'check if found in First, Last and Mid
        If nFind = aInputSet(First) Then
            nlocation = First
            bfound = True
        ElseIf nFind = aInputSet(Last) Then
            nlocation = Last
            bfound = True
        ElseIf nFind = aInputSet(Mid) Then
            nlocation = Mid
            bfound = True
        End If
    End Sub

    Private Sub DisplayResults()
        If bfound = True Then
             lstResults.Items.Add("Number is found on location " &
Str(nlocation))
        Else
             lstResults.Items.Add(" Number is NOT FOUND on the LIST! ")
        End If
    End Sub
End Class

More Related Content

What's hot

Ejemplo radio
Ejemplo radioEjemplo radio
Ejemplo radiolupe ga
 
Lampiran source code
Lampiran source codeLampiran source code
Lampiran source codeancunk
 
How to Write Better Code with Mutation Testing
How to Write Better Code with Mutation TestingHow to Write Better Code with Mutation Testing
How to Write Better Code with Mutation TestingJohn Backus
 
Understanding Lemon Generated Parser
Understanding Lemon Generated ParserUnderstanding Lemon Generated Parser
Understanding Lemon Generated Parservivekanandan r
 
Understanding Lemon Generated Parser Final
Understanding Lemon Generated Parser FinalUnderstanding Lemon Generated Parser Final
Understanding Lemon Generated Parser Finalvivekanandan r
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventasDAYANA RETO
 
Regexes and-performance-testing
Regexes and-performance-testingRegexes and-performance-testing
Regexes and-performance-testingdoughellmann
 
Migrating from Ext GWT 2.x to 3.0
Migrating from Ext GWT 2.x to 3.0Migrating from Ext GWT 2.x to 3.0
Migrating from Ext GWT 2.x to 3.0Sencha
 
Bhanu Pratap Singh Shekhawat, BCA Third Year
Bhanu Pratap Singh Shekhawat, BCA Third YearBhanu Pratap Singh Shekhawat, BCA Third Year
Bhanu Pratap Singh Shekhawat, BCA Third YearDezyneecole
 
Linked In Presentation
Linked In PresentationLinked In Presentation
Linked In Presentationapweir12
 
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD  MAKING IN PAYTHON BY ROHIT MALAVNOTEPAD  MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAVRohit malav
 
Ordenara los vectores
Ordenara los vectoresOrdenara los vectores
Ordenara los vectoresIsrael JD
 

What's hot (20)

Ejemplo radio
Ejemplo radioEjemplo radio
Ejemplo radio
 
Lampiran source code
Lampiran source codeLampiran source code
Lampiran source code
 
How to Write Better Code with Mutation Testing
How to Write Better Code with Mutation TestingHow to Write Better Code with Mutation Testing
How to Write Better Code with Mutation Testing
 
Understanding Lemon Generated Parser
Understanding Lemon Generated ParserUnderstanding Lemon Generated Parser
Understanding Lemon Generated Parser
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
Andres y keitleen
Andres y keitleenAndres y keitleen
Andres y keitleen
 
Understanding Lemon Generated Parser Final
Understanding Lemon Generated Parser FinalUnderstanding Lemon Generated Parser Final
Understanding Lemon Generated Parser Final
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
3
33
3
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
 
Regexes and-performance-testing
Regexes and-performance-testingRegexes and-performance-testing
Regexes and-performance-testing
 
Migrating from Ext GWT 2.x to 3.0
Migrating from Ext GWT 2.x to 3.0Migrating from Ext GWT 2.x to 3.0
Migrating from Ext GWT 2.x to 3.0
 
Listing program
Listing programListing program
Listing program
 
Bhanu Pratap Singh Shekhawat, BCA Third Year
Bhanu Pratap Singh Shekhawat, BCA Third YearBhanu Pratap Singh Shekhawat, BCA Third Year
Bhanu Pratap Singh Shekhawat, BCA Third Year
 
Python002
Python002Python002
Python002
 
Linked In Presentation
Linked In PresentationLinked In Presentation
Linked In Presentation
 
New text document
New text documentNew text document
New text document
 
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD  MAKING IN PAYTHON BY ROHIT MALAVNOTEPAD  MAKING IN PAYTHON BY ROHIT MALAV
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAV
 
Ordenara los vectores
Ordenara los vectoresOrdenara los vectores
Ordenara los vectores
 

Similar to Binary Search Program in Visual Basic 2008

1. Determine the output displayed when the button is clicked.Priva.docx
1. Determine the output displayed when the button is clicked.Priva.docx1. Determine the output displayed when the button is clicked.Priva.docx
1. Determine the output displayed when the button is clicked.Priva.docxcorbing9ttj
 
1. Determine the output displayed when the button is clicked. Priv.docx
1. Determine the output displayed when the button is clicked. Priv.docx1. Determine the output displayed when the button is clicked. Priv.docx
1. Determine the output displayed when the button is clicked. Priv.docxcorbing9ttj
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2ppJ. C.
 
การใช้ ListBox และ ComboBox Control
การใช้ ListBox และ ComboBox Controlการใช้ ListBox และ ComboBox Control
การใช้ ListBox และ ComboBox ControlWarawut
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
please code in c#- please note that im a complete beginner- northwind.docx
please code in c#- please note that im a complete beginner-  northwind.docxplease code in c#- please note that im a complete beginner-  northwind.docx
please code in c#- please note that im a complete beginner- northwind.docxAustinaGRPaigey
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09Terry Yoast
 
Basic controls in asp
Basic controls in aspBasic controls in asp
Basic controls in aspSireesh K
 
Write a function called countElements that counts the number of times.pdf
Write a function called countElements that counts the number of times.pdfWrite a function called countElements that counts the number of times.pdf
Write a function called countElements that counts the number of times.pdffeetshoemart
 
How do I - Create a List of Items - Transcript.pdf
How do I - Create a List of Items - Transcript.pdfHow do I - Create a List of Items - Transcript.pdf
How do I - Create a List of Items - Transcript.pdfShaiAlmog1
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfmayorothenguyenhob69
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptxNawalKishore38
 
1- The intNumbers array is a one dimensional array- Write the statemen.docx
1- The intNumbers array is a one dimensional array- Write the statemen.docx1- The intNumbers array is a one dimensional array- Write the statemen.docx
1- The intNumbers array is a one dimensional array- Write the statemen.docxtjames442
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfrajeshjangid1865
 

Similar to Binary Search Program in Visual Basic 2008 (20)

1. Determine the output displayed when the button is clicked.Priva.docx
1. Determine the output displayed when the button is clicked.Priva.docx1. Determine the output displayed when the button is clicked.Priva.docx
1. Determine the output displayed when the button is clicked.Priva.docx
 
1. Determine the output displayed when the button is clicked. Priv.docx
1. Determine the output displayed when the button is clicked. Priv.docx1. Determine the output displayed when the button is clicked. Priv.docx
1. Determine the output displayed when the button is clicked. Priv.docx
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
การใช้ ListBox และ ComboBox Control
การใช้ ListBox และ ComboBox Controlการใช้ ListBox และ ComboBox Control
การใช้ ListBox และ ComboBox Control
 
Docimp
DocimpDocimp
Docimp
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
ECE-PYTHON.docx
ECE-PYTHON.docxECE-PYTHON.docx
ECE-PYTHON.docx
 
please code in c#- please note that im a complete beginner- northwind.docx
please code in c#- please note that im a complete beginner-  northwind.docxplease code in c#- please note that im a complete beginner-  northwind.docx
please code in c#- please note that im a complete beginner- northwind.docx
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
VB net lab.pdf
VB net lab.pdfVB net lab.pdf
VB net lab.pdf
 
Basic controls in asp
Basic controls in aspBasic controls in asp
Basic controls in asp
 
python.pdf
python.pdfpython.pdf
python.pdf
 
Robust Python.pptx
Robust Python.pptxRobust Python.pptx
Robust Python.pptx
 
Write a function called countElements that counts the number of times.pdf
Write a function called countElements that counts the number of times.pdfWrite a function called countElements that counts the number of times.pdf
Write a function called countElements that counts the number of times.pdf
 
How do I - Create a List of Items - Transcript.pdf
How do I - Create a List of Items - Transcript.pdfHow do I - Create a List of Items - Transcript.pdf
How do I - Create a List of Items - Transcript.pdf
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdf
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
1- The intNumbers array is a one dimensional array- Write the statemen.docx
1- The intNumbers array is a one dimensional array- Write the statemen.docx1- The intNumbers array is a one dimensional array- Write the statemen.docx
1- The intNumbers array is a one dimensional array- Write the statemen.docx
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 

Binary Search Program in Visual Basic 2008

  • 1. A Program in Binary Searching Using Visual Basic 2008 (version 9.0) By Ariel T. Sumagaysay, MSCS A. Form Design Design the form shown below using the Visual Basic 2008: B. Components Components Properties / Settings Form1 Name: frmMain Font: Times New Roman Font Style: Regular Size: 10 FormBorderStyle: FixedDialog Icon: Any MaximizeBox: False StartPosition: CenterScreen Text: BINARY SEARCH PLUS GroupBox1 Name: GroupBox1 Text: Label1 Name: Label1 Text: ENTER NUMBER OF ELEMENTS [AT LEAST 10]: Label2 Name: Label2 Text: ENTERED ELEMENTS Label3 Name: Label3 Text: ARRANGED: Label4 Name: Label4 Text: Results
  • 2. 2 B. Components (Continued…) Components Properties / Settings List1 Name: lstResults Font: Times New Roman Size: 13 TextBox1 Name: txtNumberOfElements TextBox2 Name: txtEnterElements Textbox3 Name: txtAllElements Font: Times New Roman Font Style: Regular Size: 14 Multiline: True ReadOnly: True ScrollBars: Both WordWarp: False Textbox4 Name: txtArranged Font: Times New Roman Font Style: Regular Size: 14 Multiline: True ReadOnly: True ScrollBars: Both WordWarp: False Textbox5 Name: txtEnterSearch Font: Times New Roman Font Style: Regular Size: 14 Button1 Name: btnNumber Text: &OK Button2 Name: btnEnterElement Text: &ENTER ELEMENT Button3 Name: btnSearch Image: any *.PNG related graphics Text: &SEARCH TextImageRelation: ImageAboveText Button4 Name: btnReset Image: any *.PNG related graphics Text: &RESET TextImageRelation: ImageAboveText Button5 Name: btnQuit Image: any *.PNG related graphics Text: &QUIT TextImageRelation: ImageAboveText
  • 3. 3 C. Code Listings 'BINARY SEARCH 'PROGRAM BY ARIEL T. SUMAGAYSAY 'COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY 'WEST NEGROS UNIVERSITY 'APRIL 04, 2010 1658 HR 'Limitations of the Program:The list must not have duplicate values Option Explicit Off Public Class frmMain 'Variables Dim nNumberOfElements As Integer Dim nResetStatus As Integer Dim sNumbers As String Dim nFind As Integer Dim bfound As Boolean Dim nlocation As Integer Dim lowBound As Integer Dim HighBound As Integer Dim nDivTimes As Integer Dim nDTCtr As Integer Dim n As Integer Dim x As Integer Dim s As String 'One Dimensional Array Dim aInputSet() As Integer Private Sub btnNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNumber.Click 'Get Number of Elements nNumberOfElements = Int(txtNumberOfElements.Text) If nNumberOfElements < 10 Then txtNumberOfElements.Enabled = False btnNumber.Enabled = False MsgBox("Number of Elements entered is less than 10") MsgBox("Click RESET button to enter number again") nResetStatus = 1 btnReset.Enabled = True Else 'Setting Buttons btnEnterElement.Enabled = True btnNumber.Enabled = False txtEnterElement.Enabled = True txtNumberOfElements.Enabled = False 'Set the number of elements for the array
  • 4. 4 n = nNumberOfElements ReDim aInputSet(nNumberOfElements) 'set command button label s = Format(1, "0#") btnEnterElement.Text = "&ENTER ELEMENT " + s End If 'Compute Number of time in Division by 2 nDivTimes = System.Math.Truncate(System.Math.Sqrt(nNumberOfElements)) End Sub Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Initialize Variables nNumberOfElements = 0 nResetStatus = 0 nDivTimes = 0 nDTCtr = 0 'Initial Settings txtNumberOfElements.Text = " " txtAllElements.Text = " " txtArranged.Text = " " txtEnterElement.Text = " " txtEnterSearch.Text = "0" bfound = False lstResults.Items.Clear() 'Setting Buttons - Controls btnEnterElement.Enabled = False btnSearch.Enabled = False btnReset.Enabled = False txtNumberOfElements.Enabled = True txtEnterElement.Enabled = False txtEnterSearch.Enabled = False 'Initialize variables nNumberOfElements = 0 n = 0 x = 0 s = " " sNumbers = " " 'Set string of numbers to NULL nFind = 0 'Set the Number of Elements ReDim aInputSet(0) End Sub
  • 5. 5 Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click 'For Enter less than 10 elements If nResetStatus = 1 Then txtNumberOfElements.Text = 0 txtNumberOfElements.Enabled = True btnNumber.Enabled = True End If 'For Resetting the values If nResetStatus = 2 Then 'Initialize Variables nNumberOfElements = 0 nResetStatus = 0 nDivTimes = 0 nDTCtr = 0 'Initial Settings txtNumberOfElements.Text = " " txtAllElements.Text = " " txtArranged.Text = " " txtEnterElement.Text = " " txtEnterSearch.Text = "0" bfound = False lstResults.Items.Clear() 'Setting Buttons - Controls btnEnterElement.Enabled = False btnSearch.Enabled = False btnReset.Enabled = False btnNumber.Enabled = True txtNumberOfElements.Enabled = True txtEnterElement.Enabled = False txtEnterSearch.Enabled = False 'Initialize variables nNumberOfElements = 0 n = 0 x = 0 s = " " sNumbers = " " 'Set string of numbers to NULL nFind = 0 'Set the Number of Elements ReDim aInputSet(0) End If End Sub
  • 6. 6 Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Static y As Integer Static mid As Integer 'Get the mid point y = nNumberOfElements mid = y / 2 'Search Border Values Call BorderValues(1, y, mid) 'Call the Binary Search Procedure 'binarySearch(First, Last, Mid) If bfound = False Then Call binarySearch(1, y, mid) End If 'Display Results Call DisplayResults() 'Set Reset Status nResetStatus = 2 btnSearch.Enabled = False End Sub Private Sub btnEnterElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterElement.Click Static y As Integer y = 0 'Enter numbers If txtEnterElement.Text <> " " Then x = x + 1 y = x s = Format(y + 1, "0#") btnEnterElement.Text = "&ENTER ELEMENT " + s aInputSet(x) = CInt(txtEnterElement.Text) If x = n Then btnEnterElement.Enabled = False txtEnterElement.Enabled = False btnEnterElement.Text = "&ENTER ELEMENT" Call bubblesort() End If sNumbers = sNumbers & txtEnterElement.Text & " " txtAllElements.Text = sNumbers txtEnterElement.Text = " " End If 'Set Reset Status nResetStatus = 2 btnReset.Enabled = True 'Enable Search Features txtEnterSearch.Enabled = True End Sub
  • 7. 7 Private Sub bubblesort() 'Sort the elements using bubblesort Static a As Integer Static b As Integer Static temp As Integer Static sHereNumbers As String 'initialize variables a = 0 b = 0 temp = 0 sHereNumbers = " " For a = 1 To n For b = 1 To n - 1 If (aInputSet(b) > aInputSet(b + 1)) Then temp = aInputSet(b) aInputSet(b) = aInputSet(b + 1) aInputSet(b + 1) = temp End If Next Next 'display list For a = 1 To n sHereNumbers = sHereNumbers + Str(aInputSet(a)) & " " txtArranged.Text = sHereNumbers Next End Sub Private Sub txtEnterSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEnterSearch.TextChanged 'Get value to search nFind = Int(txtEnterSearch.Text) 'Enabled Search Button btnSearch.Enabled = True End Sub Private Sub binarySearch(ByVal First As Integer, ByVal Last As Integer, ByVal Mid As Integer) 'Counter nDTCtr = nDTCtr + 1 If nDTCtr > nDivTimes Then Exit Sub End If 'Binary Search Procedure If nFind > aInputSet(Mid) Then lowBound = Mid HighBound = Last lstResults.Items.Add(Str(nFind) & " is possibly located between location " & Str(lowBound) & " and " & Str(HighBound))
  • 8. 8 lstResults.Items.Add(Str(nFind) & " is possibly located between values " & aInputSet(lowBound) & " and " & aInputSet(HighBound)) lstResults.Items.Add(" ") Mid = (lowBound + HighBound) / 2 Call binarySearch(lowBound, HighBound, Mid) ElseIf nFind < aInputSet(Mid) Then lowBound = First HighBound = Mid lstResults.Items.Add(Str(nFind) & " is possibly located between location " & Str(lowBound) & " and " & Str(HighBound)) lstResults.Items.Add(Str(nFind) & " is possibly located between values " & aInputSet(lowBound) & " and " & aInputSet(HighBound)) lstResults.Items.Add(" ") Mid = (lowBound + HighBound) / 2 Call binarySearch(lowBound, HighBound, Mid) ElseIf nFind = aInputSet(Mid) Then nlocation = Mid bfound = True End If End Sub Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click 'Terminate Program End End Sub Private Sub BorderValues(ByVal First As Integer, ByVal Last As Integer, ByVal Mid As Integer) 'check if found in First, Last and Mid If nFind = aInputSet(First) Then nlocation = First bfound = True ElseIf nFind = aInputSet(Last) Then nlocation = Last bfound = True ElseIf nFind = aInputSet(Mid) Then nlocation = Mid bfound = True End If End Sub Private Sub DisplayResults() If bfound = True Then lstResults.Items.Add("Number is found on location " & Str(nlocation)) Else lstResults.Items.Add(" Number is NOT FOUND on the LIST! ") End If End Sub End Class