Skip to main content

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:​

  1. Compare 5 with 4 β†’ ❌
  2. Compare 2 with 4 β†’ ❌
  3. Compare 9 with 4 β†’ ❌
  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​

CaseTime Complexity
Best CaseO(1)
AverageO(n)
Worst CaseO(n)
  • Space Complexity: O(1)

  • 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! πŸš€