Skip to content

Latest commit

 

History

History
56 lines (27 loc) · 842 Bytes

Big-O Notation.md

File metadata and controls

56 lines (27 loc) · 842 Bytes

#Big-O Notation

Definition:

  • A way computer algorithm scales with large input

Big-O Notation:

  • Time Complexity
  • Space Complexity

Examples:

Problem 1 :

Take an input of n and return the sum of the numbers from 0 to n

Bad Answer:

def sum_all_nums(n):

  sum_of_nums = 0;
  
  for x in range(0,n):
  
    sum_of_nums += x
  
  return sum_of_nums

Good Answer:

def sum_all_nums(n):
  
  return (n*(n+1))/2

Explanation:

For the fist answer the (time comlexity), which means the time it takes to run the function is O(n), while the second answer

time complexity is O(1).

The reason for the second answer to be efficient is that O(1) constatnt is considered the most efficient functions among Big-O functions, because it return ONLY one statement(no loops, no ifs)