Git Product home page Git Product logo

data-structures-and-algorithm's Introduction

JAVASCRIPT DATA STRUCTURES AND ALGORITHMS

Data-structures-and-algorithms

SECTION 1 : ALGORITHMS

Chapter 1 : Big O Notation

Bog-O-Notation

  • Motivate the need for sth like Big O Notation
  • Describe what Big O Notation is
  • Simplify Big O Expressions
  • Define "time complexity" and "space complexity"
  • Evaluate the time complexity and space complexity of different algorithms using Big O Notation
  • Describe what a logarithm is

Chapter 2 : Recursion

Recursion

Objectives

  • Define what recursion is and how it can be used.
  • Understand the two essential components of a recursive function.
  • Visualize the call stack to better debug and understand recursive functions.
  • Use helper method recursion and pure recursion to solve more difficult probles.

Introduce

  • What is recursion? A process( a function in our case) that call itself

  • In almost all program languages, there is a built in data structure that manages what hanppens when functions are invoked (It's named call stack).

    • About call stack
      • It's stack in data structure.
      • Any time a function is invoke it is placed (pushed) on the top of the call stack
      • When JS sees the returns **key workd or when the function ends, the compiler will remove(pop).
      • Brieftly
        • You're used to functions being pushed on the call stack and popped off when they are done.
        • When we write recursive functions, we keep pushing new functions onto the call stack!
  • How recursive functions work?
    Invoke the same function with a different input ultil reaching to base case.

Chapter 3 : Searching Algorithms

  • How do we search? Given an array, the simplest way to search for an value is to look at every element in the array and check if it's the value we want. (called linear search)

  • JS has search!

    • There are many different search methods on arrays in javascript:
      • indexOf
      • includes
      • find
      • findIndex

Searching-Algorithms

Chapter 4 : Sorting Algorithms Fundamental

- Sorting is the process of rearanging the items in a collection(e.g array) so that the item are in some kind of order.
  • Example :
    • Sorting numbers from smallest to largest
    • Sorting names alphabetically
    • Sorting movies based on release year
    • Sorting movies based on revenue
  • Sorting is an incredibly common task, so it's good to know how it works.
  • There are many different ways to sort things, and different techniques have their own advantages and disadvantages.
  • Elementary Sort :
    1. Bubble sort.
    2. Selection sort.
    3. Insertion sort.
  • Compare Bubble, Selection, Insertion sort
  • Shortly:
    • Sorting is fundamental!
    • Bubble sort, selection sort, insertion sort, are all roughly equivalent
    • All have average time complexities that are quadratic
    • We can do better... but we need more complex algorithms!

Chapter 5 : Intermidiate Sorting Algorithms

Why?

  • The sorting algorithms we've learned so far don't scale well.
  • There is family of sorting algorithms that can improve time of complexity from O(n2) to O(nlogn).
  • The more efficient algorithms are much less simple, and generally take a longer time to understand

1.Merging Arrays

  • In order to implement merge sort, it's useful to first implement a function responsible for merging sort.
  • Given two arrays which are sorted, this helper function should create a new array, which is also sorted, and consists of all of the elements in the two input arrays.
  • This function should run in O(n + m) time and O(n + m) space and should not modify the parameters passed to it.\

2.Quick sort

  • In order to implement merge sort, it's useful to first implement a function responsible for merging sort.
  • Given two arrays which are sorted, this helper function should create a new array, which is also sorted, and consists of all of the elements in the two input arrays.
  • This function should run in O(n + m) time and O(n + m) space and
  • The order of element on either side of the pivot doesn't matter!
  • The helper should do this in place, that is, it should not create a new array
  • When complete, helper should return the index of the pivot.
  • The runtime of quick sort depends on parts on how one selects the pivot
  • Ideally, the pivot should be chosen so that it's roughly the median value in data set you are setting.
  • For simplicity, we'll always choose the pivot to be the first element.

3.Radix sort

  • Radix sort is a special sorting algorithm that works on lists of numbers
  • It nerver makes comparisions between elements
  • It exploits the fact that information about the size of a number is encoded in the number of digits
  • More digits means the bigger number!

Big O of Merge Sort

Name Time of complexity (Best) Time of complexity (Avg) Time of complexity (worst) Space complexity
Merge Sort O(n logn) O(n logn) O(n logn) O(n)
Quick Sort O(n logn) O(n logn) O(n2) O(logn)
Radix Sort O(kn) O(kn) O(kn) O(n + k)

SECTION 2 : DATA STRUCTURES

Define: Data stuctures are collections of values, the relationship among them, and the functions or operations that can be applied to the data

Different data structures excel at defferent things. Some are hightly specialized, while others (like arrays) are more generally used.

Contents

1. Class Syntax

2. Singly Linked Lists

Linked lists is a data structure that contains a head, tail and length property.

Linked lists consists of nodes, and each node has a value and a pointer to another node or null.

Comparisons with Arrays

Lists Arrays
  • Do not have indexes!
  • Connected via nodes with next pointer
  • Random access is not allowed.
  • Indexed in order!
  • Insertion and deletion can be expensive
  • Can quickly be accessed at a specific index.

Big O Notation

type time of complexity
Insertion O(1)
Removal It depends ... O(1) or O(n)
Searching O(n)
Access O(n)

RECAP:

  • Singly linked lists are excellent alternatives arrays when insertion and deletion at the beginning are frequently required
  • Array contains a build in index, whereas Linked lists do not index.
  • The idea of list data structure that consists of nodes is the foundation for other data structures like Stacks and Queues

3. Doubly Linked Lists

Doubly Linked Lists are the same as singly Linked Lists, except they have more previous pointer. Therefore, they take advantage of searching, remove the last item compare with singly linked lists, but they have to use memory more than singly linked lists.

Big O Notation

type time of complexity
Insertion O(1)
Removal O(1)
Searching O(n) [actually O(n/2)]
Access O(n)

RECAP:

  • Doubly Linked Lists are almost identical to Singly linked lists except there is an additional pointer previous nodes.
  • Better than Singly Linked List for finding nodes and can be done in half the time!
  • However, they do take up more memory considering the extra pointer.

4. Stack And Queue

Stack-and-Queue

1. Stack

Stack is a LIFO data structure which is the last element added to the stack will be the first element removed from the stack.
  • Managing function invocations.
  • Undo/ Redo
  • Routing (the history object) is treated like a stack!

Big O Notation

type time of complexity
Insertion O(1)
Removal O(1)
Searching O(n)
Access O(n)

2. Queue

Queue is a FIFO data structure which is the last element added to the queue will be the last element removed from the queue.

Big O Notation

type time of complexity
Insertion O(1)
Removal O(1)
Searching O(n)
Access O(n)

5. Binary Tree

Binary-Tree
     Binary Tree is a data structure that consists of nodes an a parent/child relationship.
     The different between List and Tree are :
       List is linear
       Tree is nonlinear

Tree Terminology

  • Root : The top node in a tree.
  • Child : A node directly connected to another node when moving away from the Root
  • Parent : The converse notion of a child.
  • Siblings : A group of nodes with the same parent.
  • Leaf : A node with no children.
  • Edge : The connection between one node and another.

Applications

  • HTML DOM
  • Network Routing
  • Abstract syntax tree
  • Artificial Intelligence, Machine Learning
  • Folders in Operation Systems
  • Computer File Science

How Binary Search Trees work

  • Every parent node has at most two children
  • Every node to the left of a parent node is always less than the parent
  • Every node to the right of a parent node is always greater than the parent

Big O Notation

type time of complexity
Insertion O(log n)
Searching O(log n)

Tree Traversal

  • Two ways :
    • Breadth-first Search
    • Depth-first Search

RECAP

  • Trees are non-linear data structures that contain a root and child nodes
  • Binary Trees can have values of any type, but at most two children for each parent
  • Binary Search Trees are a more specific version of binary trees where every node to the left of a parent is less than it's value and every node to the right is greater
  • We can search through Trees using BFS and DFS

data-structures-and-algorithm's People

Contributors

mthang1801 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.