0% found this document useful (0 votes)
5 views

Linear and Binary Search Program Code

This document contains two programs written in Visual Basic: one for linear search and another for binary search. Both programs prompt the user to input values into an array and then search for a specified value, indicating whether the value was found and at which index. The linear search iterates through the array sequentially, while the binary search uses a divide-and-conquer approach to locate the value more efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Linear and Binary Search Program Code

This document contains two programs written in Visual Basic: one for linear search and another for binary search. Both programs prompt the user to input values into an array and then search for a specified value, indicating whether the value was found and at which index. The linear search iterates through the array sequentially, while the binary search uses a divide-and-conquer approach to locate the value more efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Linear Search Program Code

Imports System.Console

Module Module1

Sub Main()

Dim List(5) As Integer

Dim Found As Boolean = False

Dim Index As Integer = 1

For C = 1 To 5 'Populating the array

Write("Enter a value: ")

List(C) = ReadLine()

Next

WriteLine() ' giving a line space

Write("Enter a value to search: ") 'Inputting value to search

Dim SearchItem As Integer = ReadLine()

While Found = False And Index <= 8

If SearchItem = List(Index) Then

Found = True

Else

Index = Index + 1

End If

End While

If Found = True Then

Write("Data was matched at: " & Index)


Else

Write("Data was not matched")

End If

ReadKey()

End Sub

End Module
Binary Search Program Code
Imports System.Console

Module Module1

Sub Main()

Dim List(5) As Integer

Dim Found As Boolean = False

Dim Index As Integer = 1

Dim First, Last, Mid As Integer

First = 1

Last = 5

For C = 1 To 5 'Populating the array

Write("Enter a value: ")

List(C) = ReadLine()

Next

WriteLine() ' giving a line space

Write("Enter a value to search: ") 'Inputting value to search

Dim SearchItem As Integer = ReadLine()

While Found = False And First <= Last

Mid = Int((First + Last) / 2)

If SearchItem = List(Mid) Then

Found = True

Else

If SearchItem > List(Mid) Then

First = Mid + 1
Else

Last = Mid - 1

End If

End If

End While

WriteLine()

If Found = True Then

Write("Data was matched at: " & Mid)

Else

Write("Data was not matched")

End If

ReadKey()

End Sub

End Module

You might also like