Linear search checks elements in their original order until it finds the sought value or reaches the end. It is the simplest natural search, but may inspect every element. section 2.1
Searching a gene
Represent a gene as an array of codons, then compare each codon with the target. For example, linearContains(gene, item: acg) returns true when the codon (.A, .C, .G) appears and false when the scan finishes without a match. func linearContains(_ array: Gene, item: Codon) -> Bool { for element in array where item == element { return true } return false } section 2.1
Use linear search when:
- The data structure is not known to be sorted, so sequential inspection is the direct approach. section 2.1
- The collection is small or simplicity matters more than reducing comparisons. section 2.1
- A built-in Swift Sequence method is appropriate: contains() already performs a linear search and usually should be preferred. section 2.1
- Performance needs comparison with binary search; timing million-element arrays makes the difference observable. section 2.5 section 2.4




