Code Monkey home page Code Monkey logo

datastructures's Introduction

Searching and Sorting

Algorithm

Necessary set of instructions for performing a computation. Input, Output, Definiteness, Correctness, Finite

Time Complexity - time used by a computer to solve a problem using the algorithm.
Space Complexity - amount of computer memory required to implement the algorithm

Searching

Basic - To find an item in a set of distinct items.

Types

Linear/ Sequential
Involves looking at each value and when value is located, the program quits and return true.

Time Complexity

Best - Ω(1); Worst - O(n); Average - θ(n);

Binary
Search a sorted array by repeatedly dividing the search interval in half.

Time Complexity

Best - Ω(1); Worst - O(log N); Average - θ(log N);

Sorting

Basic - Ordering the elements of a list by any context. Telephone Directory, Song Loader

Adaptive and Non-Adaptive Sorting Algorithm
Adaptive if it takes advantage of already sorted elements and vice versa

Types

Selection
The idea behind selection sort is:

  • Find the smallest value in A; put it in A[0].
  • Find the second smallest value in A; put it in A[1]. etc.

Time Complexity

Best - Ω(n^2); Worst - O(n^2); Average - θ(n^2);

Bubble
Bubble sort is a simple sorting algorithm.

  • This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order.

Time Complexity

Best - Ω(n); Worst - O(n^2); Average - θ(n^2);

Insertion
The idea behind insertion sort is:

  • Put the first 2 items in proper order.
  • Insert the 3rd item in proper place relative to first 2.
  • Insert the 4th item in proper place relative to first 3.

Time Complexity

Best - Ω(n); Worst - O(n^2); Average - θ(n^2);

Quick
Choose a pivot value.

  • Partition the array (put all value less than the pivot in the left part of the array, then the pivot itself, then all values greater than the pivot).
  • Recursively, sort the values less than the pivot.
  • Recursively, sort the values greater than the pivot.

Time Complexity

Best - Ω(nlog(n)); Worst - O(n^2); Average - θ(nlog(n));

Merge
To sort an array of length N:

  • Divide the array into two halves.
  • Recursively, sort the left half.
  • Recursively, sort the right half.
  • Merge the two sorted halves.

Time Complexity

Best - Ω(nlog(n)); Worst - O(nlog(n)); Average - θ(nlog(n));

Comparison between merge and quick sort

https://techdifferences.com/difference-between-quick-sort-and-merge-sort.html

datastructures's People

Contributors

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