Git Product home page Git Product logo

js-dsa's Introduction

JavaScript Data Structures & Algorithms


Big O

Complexity

  • Time Complexity⭐️
  • Space Complexity (Memory Usage)

Cases

  • Ω : Best Case
  • θ : Average Case
  • O : Worst Case

O(n)

  • leanear. proportional
function logItems(n){
  for(let i = 0; i < n; i++){
    console.log(i)
  }
}

logItems(10);

Drop Constants

  • 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);

O(n2)

  • O(n2)
function logItems(n){
  for(let i = 0; i < n; i++){
    for(let j = 0; j < n; j++){
      console.log(i, j);
    }
  }
}

logItems(10);
  • O(n3) -> O(n2)
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);

Drop Non-Dominants

  • O(n2 + n) -> O(n2)
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);

O(1)

  • constant time
  • the most efficient
function addItems(n){
  return n + n + n;
}

addItems(3);

O(log n)

  • see half of half of half...
  • log21,073,741,824 -> look up 31 times only

O(n log n)

  • Some Sorting Algorithms

Different Terms for Input

  • O(a + b)
  • O(a * b)
function logItems(a, b){
  for(let i = 0; i < a; i++){
    console.log(i);
  }

  for(let j = 0; j < b; j++){
    console.log(j);
  }
}

Arrays

  • push, pop : O(1)
    • X re-indexed
  • shift, unshift : O(n)
    • re-indexed
  • splice : O(n)
    • re-indexed
  • find
    • search by value : O(n)
    • search by index : O(1)
  • Arrays for mutation may be not good choice

Wrap up

  • O(n2) : Loop within a loop
  • O(n) : Proportional
  • O(log n) : Divide and Conquer
  • O(1) : Constant

CheatSheet for Big O


LinkedList

  • no index
  • not continuously exists
  • node, head, tail(pointing null)

js-dsa's People

Contributors

urbanscratcher avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.