Linear Search
Linear Search is one of the simplest and most intuitive algorithms used to find an element in a list or array. It checks each element sequentially until the desired value is found or the list ends.
π How Linear Search Worksβ
Given a list of elements, linear search goes from left to right, comparing each element with the target. If a match is found, it returns the index. Otherwise, it returns -1.
π Exampleβ
Let's say we have the following array:
[5, 2, 9, 4, 7]
We want to search for the element 4
.
Step-by-step:β
- Compare 5 with 4 β β
- Compare 2 with 4 β β
- Compare 9 with 4 β β
- Compare 4 with 4 β β
Match found at index
3
π§ Go Implementationβ
Hereβs a simple Go program that performs linear search:
package main
import "fmt"
func linearSearch(arr []int, target int) int {
for i, val := range arr {
if val == target {
return i
}
}
return -1 // Not found
}
func main() {
nums := []int{5, 2, 9, 4, 7}
target := 4
index := linearSearch(nums, target)
if index != -1 {
fmt.Printf("Element found at index %d\n", index)
} else {
fmt.Println("Element not found")
}
}
π§ͺ Outputβ
Element found at index 3
β± Time and Space Complexityβ
Case | Time Complexity |
---|---|
Best Case | O(1) |
Average | O(n) |
Worst Case | O(n) |
- Space Complexity: O(1)
β When to Use Linear Searchβ
- When the dataset is unsorted
- When the list is small
- When performance isnβt a primary concern
- When you want a quick and simple solution
π Conclusionβ
Linear search is a basic yet powerful algorithm, ideal for simple scenarios or when working with small, unsorted data. Though not the most efficient for large datasets, itβs a great starting point when learning how search algorithms work.
You can view and clone the full code from my GitHub repo here: π Linear Search in Go
Happy Learning! π