GithubHelp home page GithubHelp logo

foo123 / sortingalgorithms Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 3.0 251 KB

Sorting Algorithms implementations in JavaScript

Home Page: https://foo123.github.io/examples/sorting-algorithms/

JavaScript 72.94% HTML 26.46% Batchfile 0.61%
sorting-algorithms sorting-algorithms-implemented complexity analysis

sortingalgorithms's Introduction

Sorting Algorithms

sort.js

Various sorting algorithms implementations in JavaScript

sort.min.js

screenshot

Sorting Series, which is also a kind of discrete optimization problem (i.e the permutation function p of 0..N-1 which maximizes 0*a[p[0]]+1*a[p[1]]+..+(N-1)*a[p[N-1]] is the permutation which sorts the array a in ascending order that is a[p[0]] <= a[p[1]] <= .. <= a[p[N-1]]), lies at the center of Computer Science and Algorithms because of its many uses.

(Ref. http://en.wikipedia.org/wiki/Sorting_algorithm)

Furthermore Sorting, in one way or another, is integral part of many other important algorithms and applications (see eg. Knuth TAOCP)

For example Sorting is very closely associated to Searching, another topic of immense importance and applications.

Under certain sorting states, searching can be achieved in O(logN) time or even in O(1) time (constant) for almost every search term.

Sorting has 3 approaches:

(eg. NIST.gov maintains a dictionary of various algorithms at: http://xlinux.nist.gov/dads// )

Block vs. Online/Adaptive:

  1. In the Block case, the whole array is available at once for this case many algorithms are known (comparison-based=> O(N^2), O(NlogN) complexities) and (number/count based=> O(N) complexity) (see below)

  2. In the Adaptive/Online case, the input series is accesed one at a time (for example an time-input signal). In this case some of the previous algorithms can be transformed to work adaptively

Apart from that, there are algorithms (like Dynamic Lists, Dynamic Heaps and Balanced Trees, Tries, eg AVL Trees) which keep an input sequence always in a 'sorted' state (with each new input) with relatively low complexity (eg O(logN))

Comparison-Based vs. Arithmetic/Count-Based:

  • Comparison-based sorting algorithms (InsertionSort, MergeSort, QuickSort, etc..) sort a series by comparing elements with each other in some optimum sense

The best time complexity of these algorithms is O(NlogN)

However better than this can be achieved

  • Statistics-based sorting algorithms (CountingSort, BucketSort, RadixSort, etc..), do not use comparisons (of any kind) between elements, but instead use their arithmetic/statistical properties

This makes possible algorithms which can sort in linear O(N) time (the fastest possible) However these algorithms have some limitations (eg only Integers, or special kinds of Numbers)

Is O(N) sorting possible for arbitrary random numbers??

Computing the value of a certain number n requires approximately O(logn) "primitive digit" operations. Since (statisticaly) the values of numbers in a list is correlated to the size of the list itself (i.e a list of size N contains random numbers in the range 0..N with very high probability over lists of same size for numbers in a given range), one then has an overall complexity of O(NlogN) even for arithmetic-based sorting algorithms (see for example "what is the true complexity of radix sort?").

Classical algorithms for integer sorting require assumptions about the size of the integers to be sorted, or else have a running time dependent on the size.

-- Sorting in Linear Time?

However the catch here is that same holds for comparing arbitrary numbers, computationaly one has to compare primitive digit by primitive digit in sequence on average, hence an additional O(logn) complexity for comparison-based algorithms.

Is O(NlogN) complexity a kind of strict base line for this computational model??

According to Knuth's theoretical lower bound theorem for general (comparison) sorting algorithms (note O(logN!) = O(NlogN)): the O(NlogN) bound is asymptoticaly tight (see also information-theoretic lower bound for comparison sorts ie Ω(NlogN) ).

A summary of various sorting/searching algorithms can be found in this pdf

Included Algorithms


NOTE: The calculation of asymptotic complexity is done usually (using recursive relations) with the Master Theorem :

Refs.
http://en.wikipedia.org/wiki/Master_theorem, http://en.wikipedia.org/wiki/Introduction_to_Algorithms

T(n) = aT(n/b) + f(n), a>=1, b>1

eg. for MergeSort => T(n) = 2T(n/2) + O(n) => T(n) = O(nlogn)


This package implements showcases of various (best known) sorting algorithms (and a couple of custom ones) for study, experimentation and use in applications In a concice library

Algorithms as a technology Suppose computers were infinitely fast and memory was free. Would you have any reason to study algorithms? The answer is yes, if for no other reason than that you would still like to demonstrate that your solution method terminates and does so with the correct answer. ...Of course, computers may be fast but not infinitely fast and memory may be cheap but not completely free. Computing time is therefore a bounded resource, and so is space in memory. These resources should be used wisely and algorithms that are efficient in terms of time and space will help you do so.
This demostrates that algorithms, like computer hardware, are a technology . Total system performance depends on choosing efficient algorithms as much as choosing fast hardware. Just as rapid advances are being made in other computer technologies, they are being made in algorithms as well. (Introduction to algorithms, 2nd Ed. Cormen,Leiserson,Rivest,Stein)

Algorithms as a "green" technology

Additionaly, every operation/instruction a computer performs has an energy consumption cost. So an efficient algorithm saves energy! An efficient algorithm performs a computation by trying to use the resources in the best possible manner, so effectively uses energy in the best possible manner. Where does energy come from? It comes from burning coal (mainly). So there you have it, efficient code is ecological! Better start learning your complexity soon.

see also:

  • Abacus advanced Combinatorics and Algebraic Number Theory Symbolic Computation library for JavaScript, Python
  • Plot.js simple and small library which can plot graphs of functions and various simple charts and can render to Canvas, SVG and plain HTML
  • HAAR.js image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)
  • HAARPHP image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)
  • FILTER.js video and image processing and computer vision Library in pure JavaScript (browser and node)
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python
  • GrammarTemplate grammar-based templating for PHP, JavaScript, Python
  • codemirror-grammar transform a formal grammar in JSON format into a syntax-highlight parser for CodeMirror editor
  • ace-grammar transform a formal grammar in JSON format into a syntax-highlight parser for ACE editor
  • prism-grammar transform a formal grammar in JSON format into a syntax-highlighter for Prism code highlighter
  • highlightjs-grammar transform a formal grammar in JSON format into a syntax-highlight mode for Highlight.js code highlighter
  • syntaxhighlighter-grammar transform a formal grammar in JSON format to a highlight brush for SyntaxHighlighter code highlighter
  • SortingAlgorithms implementations of Sorting Algorithms in JavaScript
  • PatternMatchingAlgorithms implementations of Pattern Matching Algorithms in JavaScript

sortingalgorithms's People

Contributors

foo123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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