Searching for data is one of the most common tasks in programming. Whether you are trying to find a number in an array or a word in a list, knowing how to efficiently search is essential. One of the simplest and most intuitive searching algorithms is Linear Search. Linear Search works by checking each element in a list or array one by one until the target element is found. Because of its simplicity, it is often the first search algorithm that beginners learn.
with hands-on learning.
get the skills and confidence to land your next move.
Linear Search is especially useful when dealing with unsorted data, as it does not require the list to be ordered. It is commonly used in small datasets, user input validations, and educational examples to teach how basic algorithms work. Even though it is not the most efficient for large datasets, it provides a clear understanding of how search operations function in programming.
Program 1: Linear Search Using For Loop
This program demonstrates a simple implementation of Linear Search using a for loop. It searches for a specific number in a predefined array.
Module Module1
Sub Main()
Dim data() As Integer = {5, 3, 8, 1, 9, 6}
Dim target As Integer = 8
Dim found As Boolean = False
For i As Integer = 0 To data.Length - 1
If data(i) = target Then
Console.WriteLine("Element found at position: " & i)
found = True
Exit For
End If
Next
If Not found Then
Console.WriteLine("Element not found in the array.")
End If
Console.ReadLine()
End Sub
End ModuleThe program loops through each element of the array and compares it with the target value. If the target is found, it prints the position and exits the loop. Beginners can easily understand this approach because it directly shows the step-by-step process of searching through a list.
Program 2: Linear Search Using While Loop
This example implements Linear Search using a while loop, which provides an alternative looping method.
Module Module1
Sub Main()
Dim data() As Integer = {2, 7, 4, 9, 1}
Dim target As Integer = 4
Dim i As Integer = 0
Dim found As Boolean = False
While i < data.Length
If data(i) = target Then
Console.WriteLine("Element found at position: " & i)
found = True
Exit While
End If
i += 1
End While
If Not found Then
Console.WriteLine("Element not found in the array.")
End If
Console.ReadLine()
End Sub
End ModuleHere, a while loop is used to traverse the array until the element is found or the end is reached. This version helps beginners learn different loop structures and shows that Linear Search can be implemented flexibly.
Program 3: Linear Search Using Functions
This program demonstrates how to create a reusable function for Linear Search.
Module Module1
Function LinearSearch(arr() As Integer, target As Integer) As Integer
For i As Integer = 0 To arr.Length - 1
If arr(i) = target Then
Return i
End If
Next
Return -1
End Function
Sub Main()
Dim data() As Integer = {10, 20, 30, 40, 50}
Dim target As Integer = 30
Dim result As Integer = LinearSearch(data, target)
If result <> -1 Then
Console.WriteLine("Element found at position: " & result)
Else
Console.WriteLine("Element not found in the array.")
End If
Console.ReadLine()
End Sub
End ModuleBy creating a function, you can call Linear Search multiple times on different arrays or targets without rewriting code. Functions make programs cleaner and teach beginners about modular programming.
Program 4: Linear Search for Negative Numbers
Linear Search can handle negative numbers without modification. This example searches an array containing negative and positive values.
Module Module1
Sub Main()
Dim data() As Integer = {-5, 3, -2, 8, 0}
Dim target As Integer = -2
Dim found As Boolean = False
For i As Integer = 0 To data.Length - 1
If data(i) = target Then
Console.WriteLine("Element found at position: " & i)
found = True
Exit For
End If
Next
If Not found Then
Console.WriteLine("Element not found in the array.")
End If
Console.ReadLine()
End Sub
End ModuleSince Linear Search compares values directly, it works seamlessly with negative numbers. This helps beginners understand that the algorithm is value-agnostic.
Program 5: Linear Search for Floating-Point Numbers
This version searches decimal numbers in an array.
Module Module1
Sub Main()
Dim data() As Double = {1.5, 2.7, 3.3, 4.8, 5.0}
Dim target As Double = 3.3
Dim found As Boolean = False
For i As Integer = 0 To data.Length - 1
If data(i) = target Then
Console.WriteLine("Element found at position: " & i)
found = True
Exit For
End If
Next
If Not found Then
Console.WriteLine("Element not found in the array.")
End If
Console.ReadLine()
End Sub
End ModuleLinear Search works with any numeric type, including floating-point numbers. Beginners can see that type flexibility allows the same logic to be applied across different data types.
Program 6: Linear Search with User Input
This program allows the user to enter values at runtime and searches for the target.
Module Module1
Sub Main()
Console.WriteLine("Enter numbers separated by space:")
Dim input As String = Console.ReadLine()
Dim data() As Integer = Array.ConvertAll(input.Split(" "c), Function(s) Int32.Parse(s))
Console.WriteLine("Enter number to search:")
Dim target As Integer = Int32.Parse(Console.ReadLine())
Dim found As Boolean = False
For i As Integer = 0 To data.Length - 1
If data(i) = target Then
Console.WriteLine("Element found at position: " & i)
found = True
Exit For
End If
Next
If Not found Then
Console.WriteLine("Element not found in the array.")
End If
Console.ReadLine()
End Sub
End ModuleThis introduces user input handling and demonstrates how to integrate Linear Search in interactive programs. Beginners gain confidence by seeing how search logic can work with dynamic data.
Frequently Asked Questions (FAQ)
Here are some common questions beginners ask about Linear Search:
Q1: Can Linear Search handle negative numbers?
Yes. Linear Search works with negative numbers naturally since it compares values directly.
Q2: Is Linear Search efficient for large arrays?
Linear Search is simple but has a time complexity of O(n). For large datasets, more advanced algorithms like Binary Search are more efficient if the data is sorted.
Q3: Can Linear Search work with floating-point numbers?
Yes. Linear Search can handle integers, floating-point numbers, and even strings.
Q4: Is Linear Search stable?
Yes. Linear Search does not reorder elements; it simply finds the first occurrence of the target.
Q5: Can Linear Search be implemented recursively?
Yes. A recursive version can search by comparing the first element and calling itself on the rest of the array.
Conclusion
Linear Search is a simple yet essential algorithm for beginners. It teaches how to traverse arrays, use loops, work with functions, and handle different data types. Through the examples above—using loops, functions, negative numbers, floating-point numbers, and user input—beginners can gain practical experience in searching techniques.
While Linear Search may not be the fastest algorithm for very large datasets, it provides a solid foundation for learning more advanced search algorithms like Binary Search, Jump Search, or Interpolation Search. Practicing these examples will make beginners comfortable with arrays, loops, functions, and conditional statements—all of which are essential building blocks in VB .NET programming.




