본문 바로가기

Javascript/Algorithms

Introducing Big O

https://cs.slides.com/colt_steele/big-o-notation

 

How the runtime of an algorithm grows as the inputs grow.

 

It is a way of describing the relationship between the input to a function or as it grows

and how that changes the runtime of that function.

 

The relationship between the input size and then the time relative to that input.

 

As the input and the number of n grows how does that change to reflect in the runtime.

 

https://rithmschool.github.io/function-timer-demo/

 

## Rules of Thumb

O(n)

O(1) constant runtime

O(n^2) o of n squared

 

O(n^2 + 5n + 8)

5n plus 8 is meaningless compared to n squared.

 

We have to figure out what really matters.

 

### Big O Sharthands

  1. Arithmetic operations are constant
  2. Variable assignment is constant
  3. Accessing elements in an array (by index) or object (by key) is constant
  4. In a loop, the complexity is the length of the loop times the complexity  of whatever happens inside of the loop

Time complexity by this time,

 

Now here goes space complexity.

 

Care about what repercussions that has inside the algorithm.

Focusing on what happens inside the algorithm.

 

## Space Complexity in JS

## Rules of Thumb

  • Most primitives (booleans, numbers, undefined, null) are constant space.
    We can consider it constant space. It takes up the same amount of space.
  • Strings require O(n) space (string length)
  • Reference types arrays and objects are generally O(n),
    where n is the length or the number of keys

The space is taking up is directly proportionate to N to the input array.

Can simplify it down just to the fuzziest level which is O(n).

 

## Logarithms

The inverse of exponentiation

Just like division and multiplication, logarithms and exponent exponentiation are a pair

Logarithmic time complexity is batter than others.

 

ex) certain searching algorithms, efficient sorting algorithms, recursion involved a space complexity.

 

To analyze the performance of an algorithm , we use Big O Notation.

But It is only about general trend not precise one.

'Javascript > Algorithms' 카테고리의 다른 글

Searching Algorithms  (0) 2020.12.23
Recursion  (0) 2020.12.20
Problem Solving Patterns  (0) 2020.12.09
Problem Solving Approach  (0) 2020.12.08
The Big O of Objects and Array  (0) 2020.12.06