# Big O Notation

In my last week’s blog post, I touched upon Big O Notation, but only giving the bare minimum. Today I want to talk about what Big O Notation is, and why it is used.

Big O Notation is widely used in Computer Science and Software Engineering roles to describe the complexity of an algorithm or the runtime of a program or piece of code. It is used in tandem with logarithms and has four different types. Big O Notation helps you think about time and space complexity and when it comes to programming code it is something that is very useful to learn even some of the basics. We use this notation to measure how quickly the runtime grows and we can measure the runtime by the input. Here are some examples of Big O Notation.

The most used forms of Big O Notation are O(1)= constant, O(log n)= logarithmic, O(n)= linear, and O(n²)= quadratic, and O(n³)=cubed. O(n^n) =polynomial time, but it is not used very often so we will skip it (in case you want to know more about polynomial time: Polynomial-time complexity is the running time complexity of algorithms, which runs to the order of nk. Quadratic time algorithms are certain types of polynomial-time algorithms where k = 2.).

# Constant time O(1)

O(1) does not change with respect to input space. Hence, O(1) is referred to as being *constant time*.

An exmple of an of an O(1) is:

`function exampleConstant(n) {`

return n*n;

}

# Linear time O(n)

O(n) is *linear* time and applies to algorithms that must do *n* operations in the worst-case scenario.

most its just A simple basic loop that within it we perform constant time operations. The example would be:

`function exampleLinear(n) {`

for (var i = 0 ; i < n; i++ ) {

console.log(i)

}

}

# Logarithmic time O(log(n))

A Logarithmic time function is one in which the time of execution is proportional to the logarithm of the input size.

`function log(n) {`

for (let i = 1; i < n; i*=2) {

const result = i;

console.log(result);

}

# Quadratic time O(n² )

With quadratic time algorithms, we have now entered the dark side of the time complexity.

As the name suggests, the size of the input quadratically affects the running time of the algorithm. The basic example of quadratic time is nested loops like the inflight entertainment blog from last week or this:

`for (int i = 0; i <n; i += c) {`

for (int j = 0; j < n; j += c) {

// some O(1) expressions

}

}

Big O Notation has some complexity to it, but what it does is help us with managament and runtime of programs since we always want to have our programs running fast.