Common Big-O Notations
Big-O notation is a way to measure the time and space complexity of an algorithm. It describes the upper bound of the complexity in the worst-case scenario. Let’s look into the different types of time complexities:
1. Linear Time Complexity: Big O(n) Complexity
Linear time complexity means that the running time of an algorithm grows linearly with the size of the input.
For example, consider an algorithm that traverses through an array to find a specific element:
bool findElement(int arr[], int n, int key)
{
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return true;
}
}
return false;
}
2. Logarithmic Time Complexity: Big O(log n) Complexity
Logarithmic time complexity means that the running time of an algorithm is proportional to the logarithm of the input size.
For example, a binary search algorithm has a logarithmic time complexity:
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
3. Quadratic Time Complexity: Big O(n2) Complexity
Quadratic time complexity means that the running time of an algorithm is proportional to the square of the input size.
For example, a simple bubble sort algorithm has a quadratic time complexity:
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
4. Cubic Time Complexity: Big O(n3) Complexity
Cubic time complexity means that the running time of an algorithm is proportional to the cube of the input size.
For example, a naive matrix multiplication algorithm has a cubic time complexity:
void multiply(int mat1[][N], int mat2[][N], int res[][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
res[i][j] = 0;
for (int k = 0; k < N; k++)
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
5. Polynomial Time Complexity: Big O(nk) Complexity
Polynomial time complexity refers to the time complexity of an algorithm that can be expressed as a polynomial function of the input size n. In Big O notation, an algorithm is said to have polynomial time complexity if its time complexity is O(nk), where k is a constant and represents the degree of the polynomial.
Algorithms with polynomial time complexity are generally considered efficient, as the running time grows at a reasonable rate as the input size increases. Common examples of algorithms with polynomial time complexity include linear time complexity O(n), quadratic time complexity O(n2), and cubic time complexity O(n3).
6. Exponential Time Complexity: Big O(2n) Complexity
Exponential time complexity means that the running time of an algorithm doubles with each addition to the input data set.
For example, the problem of generating all subsets of a set is of exponential time complexity:
void generateSubsets(int arr[], int n)
{
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
cout << arr[j] << " ";
}
}
cout << endl;
}
}
Factorial Time Complexity: Big O(n!) Complexity
Factorial time complexity means that the running time of an algorithm grows factorially with the size of the input. This is often seen in algorithms that generate all permutations of a set of data.
Here’s an example of a factorial time complexity algorithm, which generates all permutations of an array:
void permute(int* a, int l, int r)
{
if (l == r) {
for (int i = 0; i <= r; i++) {
cout << a[i] << " ";
}
cout << endl;
}
else {
for (int i = l; i <= r; i++) {
swap(a[l], a[i]);
permute(a, l + 1, r);
swap(a[l], a[i]); // backtrack
}
}
}
If we plot the most common Big O notation examples, we would have graph like this:
Big O Notation Tutorial – A Guide to Big O Analysis
Big O notation is a powerful tool used in computer science to describe the time complexity or space complexity of algorithms. It provides a standardized way to compare the efficiency of different algorithms in terms of their worst-case performance. Understanding Big O notation is essential for analyzing and designing efficient algorithms.
In this tutorial, we will cover the basics of Big O notation, its significance, and how to analyze the complexity of algorithms using Big O.
Table of Content
- What is Big-O Notation?
- Definition of Big-O Notation:
- Why is Big O Notation Important?
- Properties of Big O Notation
- Common Big-O Notations
- How to Determine Big O Notation?
- Mathematical Examples of Runtime Analysis
- Algorithmic Examples of Runtime Analysis
- Algorithm Classes with Number of Operations and Execution Time
- Comparison of Big O Notation, Big Ω (Omega) Notation, and Big θ (Theta) Notation
- Frequently Asked Questions about Big O Notation
Contact Us