![Learn Data Structures and Algorithms with Golang](https://wfqqreader-1252317822.image.myqcloud.com/cover/744/36698744/b_36698744.jpg)
上QQ阅读APP看书,第一时间看更新
Linear complexity
An algorithm is of linear complexity if the processing time or storage space is directly proportional to the number of input elements to be processed. In Big O notation, linear complexity is presented as O(n). String matching algorithms such as the Boyer-Moore and Ukkonen have linear complexity.
Linear complexity, O(n), is demonstrated in an algorithm as follows:
//main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt package
import (
"fmt"
)
// main method
func main() {
var m [10]int
var k int
for k = 0; k < 10; k++ {
m[k] = k * 200
fmt.Printf("Element[%d] = %d\n", k, m[k] )
}
}
Run the following commands:
go run linear_complexity.go
The following screenshot displays the output:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/13ef15ac-cb0a-4625-ac53-826e6beff0c6.png?sign=1738873459-dP6X5XOCSoWxkMRl6QEaKJ5WZk2vRpCt-0-3ef4544f8e241008d24d9969a33477c5)
Let's take a look at quadratic complexity in the next section.