GithubHelp home page GithubHelp logo

sort-algorithms-js's Introduction

aljs

npm npm npm

sort algorithms implementation with ability to use a compare callback similar to javascript .sort.

Contents

Install

npm install --save sort-algorithms-js

require

const {
  bubbleSort, insertionSort,
  selectionSort, radixSort,
  mergeSort, heapSort, quickSort
} = require('sort-algorithms-js');

import

import {
  bubbleSort, insertionSort,
  selectionSort, radixSort,
  mergeSort, heapSort, quickSort
} from 'sort-algorithms-js';

API

bubbleSort

runtime complexity: O(n^2)

bubbleSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
bubbleSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a); // [ 9, 7, 3, 2, 1, -1, -5 ]
Benchmark
Node v14
input sizebest timeworst time
1k0 seconds 5 ms0 seconds 9 ms
10k0 seconds 227 ms0 seconds 249 ms
50k6 seconds 411 ms7 seconds 998 ms
100k26 seconds 653 ms29 seconds 735 ms
1MโŒ

insertionSort

runtime complexity: O(n^2)

insertionSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
insertionSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a); // [ 9, 7, 3, 2, 1, -1, -5 ]
Benchmark
Node v14
input sizebest timeworst time
1k0 seconds 5 ms0 seconds 10 ms
10k0 seconds 129 ms0 seconds 145 ms
50k3 seconds 49 ms3 seconds 596 ms
100k13 seconds 575 ms16 seconds 876 ms
1MโŒ

selectionSort

runtime complexity: O(n^2)

selectionSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
selectionSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a); // [ 9, 7, 3, 2, 1, -1, -5 ]
Benchmark
Node v14
input sizebest timeworst time
1k0 seconds 4 ms0 seconds 8 ms
10k0 seconds 125 ms0 seconds 139 ms
50k2 seconds 178 ms2 seconds 302 ms
100k9 seconds 740 ms10 seconds 460 ms
1MโŒ

radixSort

Only sorts numbers in O(n*d) runtime : d is the number of digits in the largest number.

radixSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
radixSort([2, 1, 7, 3, 9, -1, -5], 'desc'); // [ 9, 7, 3, 2, 1, -1, -5 ]
radixSort([{ id: 341 }, { id: 947 }, { id: 132 }], 'asc', (obj) => obj.id); // [ { id: 132 }, { id: 341 }, { id: 947 } ]
radixSort([{ id: 341 }, { id: 947 }, { id: 132 }], 'desc', (obj) => obj.id); // [ { id: 947 }, { id: 341 }, { id: 132 } ]
Benchmark
Node v14
input sizebest timeworst time
10k0 seconds 21 ms0 seconds 30 ms
50k0 seconds 61 ms0 seconds 81 ms
100k0 seconds 97 ms0 seconds 115 ms
1M1 seconds 27 ms1 seconds 103 ms
10M13 seconds 844 ms17 seconds 257 ms
50MโŒ

heapSort

runtime complexity: O(n*log(n))

heapSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
heapSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a); // [ 9, 7, 3, 2, 1, -1, -5 ]
Benchmark
Node v14
input sizebest timeworst time
10k0 seconds 12 ms0 seconds 14 ms
50k0 seconds 21 ms0 seconds 25 ms
100k0 seconds 31 ms0 seconds 44 ms
1M0 seconds 283 ms0 seconds 313 ms
10M5 seconds 219 ms6 seconds 367 ms
50M34 seconds 21 ms46 seconds 167 ms
100M76 seconds 485 ms87 seconds 991 ms

mergeSort

runtime complexity: O(n*log(n))

mergeSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
mergeSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a); // [ 9, 7, 3, 2, 1, -1, -5 ]
Benchmark
Node v14
input sizebest timeworst time
10k0 seconds 16 ms0 seconds 23 ms
50k0 seconds 38 ms0 seconds 45 ms
100k0 seconds 54 ms0 seconds 60 ms
1M0 seconds 413 ms0 seconds 435 ms
10M5 seconds 78 ms6 seconds 712 ms
50M33 seconds 229 ms35 seconds 659 ms
100M82 seconds 777 ms86 seconds 194 ms

quickSort

runtime complexity: O(n*log(n))

quickSort([2, 1, 7, 3, 9, -1, -5]); // [ -5, -1, 1, 2, 3, 7, 9 ]
quickSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a); // [ 9, 7, 3, 2, 1, -1, -5 ]
Benchmark
Node v14
input sizebest timeworst time
10k0 seconds 6 ms0 seconds 13 ms
50k0 seconds 18 ms0 seconds 26 ms
100k0 seconds 26 ms0 seconds 34 ms
1M0 seconds 167 ms0 seconds 187 ms
10M1 seconds 831 ms2 seconds 188 ms
50M10 seconds 402 ms14 seconds 777 ms
100M24 seconds 253 ms34 seconds 705 ms

Build

grunt build

License

The MIT License. Full License is here

sort-algorithms-js's People

Contributors

eyas-ranjous avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bgbruno

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.