Learn Data Structures and Algorithms with Golang
上QQ阅读APP看书,第一时间看更新

Quadratic complexity

An algorithm is of quadratic complexity if the processing time is proportional to the square of the number of input elements. In the following case, the complexity of the algorithm is 10*10 = 100. The two loops have a maximum of 10. The quadratic complexity for a multiplication table of n elements is O(n2).

Quadratic complexity, O(n2), is shown in the following example:

//main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt package
import (
"fmt"
)
// main method
func main() {
var k,l int
for k = 1; k <= 10; k++ {
fmt.Println(" Multiplication Table", k)
for l=1; l <= 10; l++ {
var x int = l *k
fmt.Println(x)
}
}
}

Run the following commands:

go run quadratic_complexity.go

The following screenshot displays the output:

Let's take a look at cubic complexity in the next section.