DEV Community

Jay Cruz
Jay Cruz

Posted on • Originally published at jay-cruz.Medium on

2

Big O Notation

Image of O and n Scrabble game squares

What is Big O?

Big O notation is a mathematical tool commonly used in Computer Science for measuring the runtimes of computer algorithms and describing their complexity. It’s often used to determine what the efficiency of a particular approach to a problem will be. More specifically, Big O determines what the “worst-case scenario” is for an algorithm’s runtime and space as the input size grows.

Time Complexity

Instead of measuring the actual time of an algorithm, we use time complexity which is the method of determining how many times an algorithm’s statements will run.

Most common time complexities

The n in O() represents the input size.

O(1) — Constant runtime. This means the code executes instantly. No matter what the input size it executes in the same exact time. For example, if a group of programmers all decide to say hello world at the same time it would cost the same amount of time as only one programmer saying hello world. The following code executes in constant time:

public void greet() {
System.out.println("Hello World!");
}
view raw constant.java hosted with ❤ by GitHub

O(n) — Linear runtime. Linear means to execute n amount of times. For example, if you can picture a long line of programmers standing in line for a job interview. Say you need to find out all of their names. You would go and ask each programmer their name individually. The following code executes in linear time:

public void askNames(int numNames) {
for (int i = 0; i < numNames; i++) {
System.out.println("What's your name?");
}
}
view raw linear.java hosted with ❤ by GitHub

O(n²) — Quadratic time. Quadratic means the runtime number of executions increases in relation to the square root of n. This is not an ideal runtime as it will only grow as the input size increases. An example in code would be nested for loop as follows:

public void printNamePairs(String[] names) {
for (int first = 0; first < names.length; first++) {
for (int last = 0; last < names.length; last++) {
System.out.println(names[first] + ", " + names[last]);
}
}
}
view raw quadratic.java hosted with ❤ by GitHub

O(log n) — Logarithmic time. This means the runtime will increase in relation to the logarithm of n (the input size). A real-world example of this runtime would be searching a phonebook for a specific number. You open the phonebook halfway then if you don't find it, repeat the process of halving the pages of only the section that needs to be checked until you eventually find the number you’re looking for. This would be considered binary search, which is a common algorithm that runs in logarithmic time. Learn more about logarithms here

public static int binarySearch(int [] phoneBook, int number) {
int mid, low, high;
low = 0;
high = phoneBook.length - 1;
while (high >= low) {
mid = (high + low) / 2;
if (phoneBook[mid] < number) {
low = mid + 1;
}
else if (phoneBook[mid] > number) {
high = mid - 1;
}
else {
return mid;
}
}
return -1; // If not found
}

Drop the Constants and Non-Dominant Terms

When determining a Big O notation runtime, the constants should be dropped. For example, if you have a specific runtime expression O(n + 1 + 1 + 1) this would be boiled down to O(n) because Big O only describes the worst-case and including the constants wouldn’t make a big enough difference. Also, non-dominant terms should be dropped from the expression, for example, O(n² + n) would become O(n²)

Space Complexity

In Big O space complexity describes the total amount of memory space used by an algorithm determined by its input size n. This is also expressed the same as time complexity for example an array of size n elements would be expressed as O(n) space.

Analyzing Space Complexity

public double calculateAverage(int[] array) {
int sum = 0;
double average;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
average = sum / array.length;
return average;
}

To determine the space complexity of this code, we can list the size in bytes of the variables used.

int = 4 bytes, double = 8 bytes

The space for this example code would be measured by 4n + 4 + 8 + 4. Since n is dominant this would evaluate to O(n) space. Read more about Space Complexity here

Additional Big O Resources

https://en.wikipedia.org/wiki/Big_O_notation

What is Big O Notation Explained: Space and Time Complexity

https://www.geeksforgeeks.org/understanding-time-complexity-simple-examples/

Originally published at https://coderjay06.github.io on October 15, 2020.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay