JavaScript Data Structures & Algorithms
- Time Complexity⭐️
- Space Complexity (Memory Usage)
- Ω : Best Case
- θ : Average Case
- O : Worst Case
function logItems(n){
for(let i = 0; i < n; i++){
console.log(i)
}
}
logItems(10);
- O(2n) -> drop constants -> O(n)
function logItems(n){
for(let i = 0; i < n; i++){
console.log(i)
}
for(let j = 0; j < n; j++){
console.log(j)
}
}
logItems(3);
function logItems(n){
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
console.log(i, j);
}
}
}
logItems(10);
function logItems(n){
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
for(let k = 0; k < n; k++){
console.log(i, j, k);
}
}
}
}
logItems(10);
function logItems(n){
for(let i = 0; i < n; i++){
for(let j = 0; j < n; j++){
console.log(k);
}
}
for(let k = 0; k < n; k++){
console.log(k);
}
}
logItems(10);
- constant time
- the most efficient
function addItems(n){
return n + n + n;
}
addItems(3);
- see half of half of half...
- log21,073,741,824 -> look up 31 times only
Different Terms for Input
function logItems(a, b){
for(let i = 0; i < a; i++){
console.log(i);
}
for(let j = 0; j < b; j++){
console.log(j);
}
}
- push, pop : O(1)
- shift, unshift : O(n)
- splice : O(n)
- find
- search by value : O(n)
- search by index : O(1)
- Arrays for mutation may be not good choice
- O(n2) : Loop within a loop
- O(n) : Proportional
- O(log n) : Divide and Conquer
- O(1) : Constant
- no index
- not continuously exists
- node, head, tail(pointing null)