Introduction to Big O

Big O Notation

Big O is defined as the asymptotic upper limit of a function. In plain english, it means that is a function that cover the maximum values a function could take. As we saw a little earlier this notation help us to predict performance and compare algorithms.
Growth RateName
1Constant
log(n)Logarithmic
nLinear
n log(n)Linearithmic
n^2Quadratic
n^3Cubic
2^nExponential
This is kinda abstract let’s see what it means in code:
Growth RateNameCode Exampledescription
1Constant
a= b + 1;
statement (one line of code)
log(n)Logarithmic
      while(n>1){
        n=n/2;
      }
      
Divide in half (binary search)
nLinear
for(c=0; c<n; c++){
  a+=1;
}
Loop
n*log(n)LinearithmicMergesort, Quicksort, …Effective sorting algorithms
n^2Quadratic
for(c=0; c<n; c++){
  for(i=0; i<n; i++){
    a+=1;
  }
}
Double loop
n^3Cubic
for(c=0; c<n; c++){
  for(i=0; i<n; i++){
    for(x=0; x<n; x++){
      a+=1;
    }
  }
}
Triple loop
2^nExponentialTrying to braeak a password generating all possible combinationsExhaustive search

Comments

Post a Comment

Popular posts from this blog

Forget Efficiency and start solving easier problems

Good schedule to follow for becoming better at competitive programming for beginners

New practice session for all the programmers with me.