NOTE
3.10 Linear Search
Linear search and its implementation.
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))
}
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub