DevLeague Coding Bootcamp
DevLeague is a Full Stack Coding Bootcamp
Understanding relative representation of the complexity of an algorithm.
Big O complexity can be visualized with this graph
*listed in order of complexity
constant complexity
// For example, accessing any single element in an array takes
// constant time as only one operation has to be performed to locate it.
const arr = [ 'accessing', 'an', 'array', 'element', 'takes', 'constant', 'time']
const index = arr[4] // return 'takes'
Linear complexity
// For example, a procedure that
// adds up all elements
// of a list requires time
// proportional to the length of the list.
const start = new Date().getTime();
const arr = []
let j = 0;
while ( j < 100000 ) {
arr.push(j);
j++;
}
let sum = 0;
for ( let i = 0; i < arr.length; i++ ) {
sum += arr100[i];
}
const end = new Date().getTime();
const time = end - start;
console.log('Execution time: ' + time);
quadratic time
// This is common with algorithms
// that involve nested iterations
// over the data set. Deeper nested
// iterations will result in O(N3), O(N4) etc.
const grid = document.createElement('table');
grid.className = 'grid';
for (let r = 0; r < this._rows; ++r) {
const tr = grid.appendChild(document.createElement('tr'));
for (let c = 0; c < this._cols; ++c) {
const cell = tr.appendChild(document.createElement('td'));
cell.id = "grid" + i++
}
}
exponential time
// An example of this is trying to break a password by testing
// every possible combination (assuming numerical
// password of length N). This results in O(10^N) complexity.
logarithmic time
Continued...
// An example of logarithmic algorithm is an algorithm that cuts
// a string in half, then cuts the right half in half, and so on. It will take
// O(log n) time (n being the length of the string) since we chop the string
// in half before each print
// Function to recursively print the right half of a string
const right = function(str) {
const length = str.length;
// Helper function
const help = function(index){
// Recursive Case: Print right half
if (index < length){
// Prints characters from index until the end of the array
console.log(str.substring(index, length));
// Recursive Call: call help on right half
help(Math.ceil((length + index) / 2));
}
// Base Case: Do Nothing
}
help(0);
}
By DevLeague Coding Bootcamp
Understanding relative representation of the complexity of an algorithm.