C# Program for Linear Search with Example and Explanation
A C# program for linear search uses a
for loop to check each element in an array until it finds the target value, like for (int i = 0; i < arr.Length; i++) { if (arr[i] == target) return i; }.Examples
Inputarray = {1, 2, 3, 4, 5}, target = 3
OutputElement found at index 2
Inputarray = {10, 20, 30, 40}, target = 25
OutputElement not found
Inputarray = {}, target = 1
OutputElement not found
How to Think About It
To do a linear search, start from the first item in the list and check if it matches the target. If it does, stop and return the position. If not, move to the next item and repeat until you find the target or reach the end.
Algorithm
1
Get the array and the target value.2
Start from the first element in the array.3
Compare the current element with the target.4
If they match, return the current index.5
If not, move to the next element.6
If the end is reached without a match, return -1.Code
csharp
using System; class Program { static int LinearSearch(int[] arr, int target) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == target) { return i; } } return -1; } static void Main() { int[] numbers = { 5, 3, 8, 4, 2 }; int target = 4; int result = LinearSearch(numbers, target); if (result != -1) { Console.WriteLine("Element found at index " + result); } else { Console.WriteLine("Element not found"); } } }
Output
Element found at index 3
Dry Run
Let's trace searching for 4 in the array {5, 3, 8, 4, 2} through the code.
1
Start loop at index 0
Check if arr[0] (5) == 4? No, continue.
2
Check index 1
Check if arr[1] (3) == 4? No, continue.
3
Check index 2
Check if arr[2] (8) == 4? No, continue.
4
Check index 3
Check if arr[3] (4) == 4? Yes, return index 3.
| Index | Value | Target | Match? |
|---|---|---|---|
| 0 | 5 | 4 | No |
| 1 | 3 | 4 | No |
| 2 | 8 | 4 | No |
| 3 | 4 | 4 | Yes |
Why This Works
Step 1: Loop through array
The for loop goes through each element one by one to check for the target.
Step 2: Compare elements
Each element is compared with the target using == to find a match.
Step 3: Return index or -1
If a match is found, the index is returned; otherwise, -1 means not found.
Alternative Approaches
Using while loop
csharp
using System; class Program { static int LinearSearch(int[] arr, int target) { int i = 0; while (i < arr.Length) { if (arr[i] == target) return i; i++; } return -1; } static void Main() { int[] numbers = { 1, 2, 3 }; int target = 2; int result = LinearSearch(numbers, target); Console.WriteLine(result != -1 ? "Element found at index " + result : "Element not found"); } }
Uses a while loop instead of for; same logic but different loop style.
Using Array.IndexOf method
csharp
using System; class Program { static void Main() { int[] numbers = { 7, 8, 9 }; int target = 8; int index = Array.IndexOf(numbers, target); if (index != -1) Console.WriteLine("Element found at index " + index); else Console.WriteLine("Element not found"); } }
Uses built-in method for simplicity but less control over search process.
Complexity: O(n) time, O(1) space
Time Complexity
The search may check each element once, so time grows linearly with array size.
Space Complexity
No extra space is needed besides variables, so space is constant.
Which Approach is Fastest?
Using Array.IndexOf is fastest for simple searches, but manual loops give more control.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop linear search | O(n) | O(1) | Simple, clear control |
| While loop linear search | O(n) | O(1) | Alternative loop style |
| Array.IndexOf method | O(n) | O(1) | Quick built-in search |
Always check if the array is empty before searching to avoid unnecessary work.
Forgetting to return -1 when the element is not found causes wrong results.