March 2020
Intermediate to advanced
406 pages
8h 39m
English
In a linear search algorithm, every element in the slice or array is checked when the slice or array is traversed sequentially. This algorithm isn't the most efficient algorithm since it ranks in at an O(n) complexity because it can traverse every element on the list.
A linear search algorithm can be written simply as an iteration through a slice, as shown in the following code snippet:
func LinearSearch(data []int, searchVal int) bool { for _, key := range data { if key == searchVal { return true } } return false}
This function shows us that it'll get expensive quickly with larger datasets. With a dataset of 10 elements, this algorithm won't take very long as it will only iterate through 10 values at a maximum. If our dataset ...
Read now
Unlock full access