What if you could find anything in a messy pile without missing a single thing?
Why Linear Search Algorithm in DSA Go?
Imagine you have a messy drawer full of different colored socks, and you want to find a red sock. You start pulling out socks one by one, checking each to see if it is red.
Looking for the red sock by checking each sock one at a time can take a long time if the drawer is very full. It is easy to lose track or get tired, making mistakes more likely.
The linear search algorithm helps by giving a simple step-by-step way to check each item until the one you want is found. It makes the search clear and organized, so you don't miss anything.
for i := 0; i < len(socks); i++ { if socks[i] == "red" { return i } } return -1
func linearSearch(items []string, target string) int {
for index, item := range items {
if item == target {
return index
}
}
return -1
}Linear search lets you find any item in a list quickly and clearly, even if the list is not sorted.
Finding a specific book on a shelf where books are placed randomly without any order.
Linear search checks each item one by one.
It works on any list, sorted or not.
It is simple but can be slow for very large lists.