Giter Site home page Giter Site logo

data-structures's Introduction

Data Structures and Algorithms

Data Structures organise and store data. Some examples of it are arrays and trees. Bounded data structures impose a storage limit, whereas unbounded are limitless.

Algorithms describe the steps you have to perform in order to accomplish a specific task. For example, sorting and search algorithms.

Big-O Notation

  • The Big-O notation is a way of expressing the time complexity of an algorithm related to the n number of items it has to process:
    • O(1): Constant
    • O(log n): Logarithmic (base 2)
    • O(n): Linear
    • O(n log n): n log-star n (base 2)
    • O(n²): Quadratic
  • When expressing time complexity, you should always refer to the worst case scenario. For example, if you have an array [1, 5, 21, -5, 3, 6, 88], and would like to find the time complexity for searching the index of the number 21, the appropriate time complexity would be O(n), regardless of whether we know we can find the desired index at the third iteration.
  • It is common for algorithms to contain small optimisations that slightly reduce the number of operations related to n as the algorithm progresses, however, we are not looking at the Big-O notation mathematically, what we want instead is an approximation to evaluate how efficient the algorithm is.
  • A tip to discover the time complexity is to look at how many loops the algorithm has, for example:
    • 1 loop: O(n)
    • 1 outer loop with an inner loop: O(n²)

The right way of reading the notation is "O of 1, O of log n", etc

Binary logarithms (log n) are the inverse function of the power of two function. They are commonly used in binary search and related algorithms.

The chart below demonstrates the relation of the n number of elements against the N time needed by different time complexity expressions:

Big-O notation time complexity chart

Recursion

  • A recursive method is a method that calls itself.
  • In order to recursion to work properly, you need a condition that ends the recursion. This condition is know as the breaking condition or base case.
  • When the recursive call satisfies the base case, one says the recursion starts to unwind.
  • However, if no base case is provided, the recursive call stack will grow indefinitely resulting in a StackOverflowError.
  • Usually, an iterative implementation (such as a for loop) runs faster and doesn't use as much memory as recursion. However, sometimes the iterative algorithm isn't as intuitive and as short as the recursive algorithm.
  • Lastly, even when you provide a base case, you can face a StackOverflowError if the number of calls is too high. Languages such as Scala support tail recursion which optimises recursive calls, however, Java does not.

data-structures's People

Contributors

bgasparotto avatar

Stargazers

 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.