Big O notation(recap) part 1
In Programming, we hear thousands of times about time complexity and space complexity. Why it is essential to understand and to study? Because in the real-world, somewhere has to store all the computers(servers, database, all hardware). So when we talk about time and space complexity we talk through what it calls Big O. Now Big o notation is not a method or sort of function. It’s simply nothing but an algorithm that descript the performance such as time and space used. So let’s talk about it.
Big Os
- O(1) Constant
no loop, O(1) or constant represent that time(or space) complexity is at constant.
function bigOconstant(){
var greeting = "hello"
console.log(greeting")
}
2. O(log N) Logarithmic
Usually, searching algorithms have log N if there are sorted such as binary search.
function bigOLogarithmic(arr, target){
var start = 0;
var end = arr.length -1; while(start <= end){
var middle = (start + end) /2;
var current = arr[middle]; if(current > target) {
start = middle - 1;
} else if(current < target){
low = middle + 1;
} else {
return middle;
}
}
3. O(n) Linear
Example like for loops, while loops through n items.
function bigOlinear(arr, target){
for(var i=0; i< arr.length; i++){
if(arr[i] === target){
retrun arr[i]
}
}
return console.log("not found!")
}