NOTE
3.10 Linear Search
Linear search compares array elements one by one, returning the target's index or -1 if it is absent. The notes include a Go implementation and test examples.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Linear Search
2. Implementation
//O(N)
func LinearSearch(data []model.Comparable, target model.Comparable) int {
for i := 0; i < len(data); i++ {
if data[i].CompareTo(target) == 0 {
return i
}
}
return -1
}
2.1. Test
func TestLinearSearch(t *testing.T) {
e1 := model.NewElement(1)
e2 := model.NewElement(2)
e3 := model.NewElement(3)
fmt.Println(LinearSearch([]model.Comparable{e1, e2, e3}, e3))
}
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub