0% found this document useful (0 votes)
10 views3 pages

Big O Notation 101

Big O concept
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Big O Notation 101

Big O concept
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Big O Notation 101: The Secret to Writing Efficient Algorithms

From simple array operations to complex sorting algorithms, understanding the Big O Notation is
critical for building high-performance software solutions.

1 - O(1)
This is the constant time notation. The runtime remains steady regardless of input size. For example,
accessing an element in an array by index and inserting/deleting an element in a hash table.
Examples:
var arr = [ 1,2,3,4,5];
arr[2]; // => 3

2 - O(n)
Linear time notation. The runtime grows in direct proportion to the input size. For example, finding the
max or min element in an unsorted array. Another good example is finding a CD in a stack of CDs or
reading a book, where n is the number of pages.
Examples in of O(n) is using linear search:
//if we used for loop to print out the values of the arrays
for (var i = 0; i < array.length; i++) { console.log(array[i]);}
var arr1 = [orange, apple, banana, lemon]; //=> 4 steps
var arr2 = [apple, htc,samsung, sony, motorola]; //=> 5 steps

3 - O(log n)
Logarithmic time notation. The runtime increases slowly as the input grows. For example, a binary
search on a sorted array and operations on balanced binary search trees.
Examples:
//Binary search implementation
var doSearch = function(array, targetValue) {
var minIndex = 0;
var maxIndex = array.length - 1;
var currentIndex; var currentElement;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = array[currentIndex];
if (currentElement < targetValue) {
minIndex = currentIndex + 1;
} else if (currentElement > targetValue) {
maxIndex = currentIndex - 1;
} else {
return currentIndex;
}
}
return -1;

//If the index of the element is not found.};


var numbers = [11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33];
doSearch(numbers, 23) //=>; 6

more examples:
for (var i = 1; i < n; i = i * 2) console.log(i);}
for (i = n; i >= 1; i = i/2) console.log(i);}
4 - O(n^2)
Quadratic time notation. The runtime grows exponentially with input size. For example, simple sorting
algorithms like bubble sort, insertion sort, and selection sort.

5 - O(n^3)
Cubic time notation. The runtime escalates rapidly as the input size increases. For example, multiplying
two dense matrices using the naive algorithm.

6 - O(n logn)
Linearithmic time notation. This is a blend of linear and logarithmic growth. For example, efficient
sorting algorithms like merge sort, quick sort, and heap sort

7 - O(2^n)
Exponential time notation. The runtime doubles with each new input element. For example, recursive
algorithms solve problems by dividing them into multiple subproblems.

8 - O(n!)
Factorial time notation. Runtime skyrockets with input size. For example, permutation-generation
problems.

9 - O(sqrt(n))
Square root time notation. Runtime increases relative to the input’s square root. For example, searching
within a range such as the Sieve of Eratosthenes for finding all primes up to n.

You might also like