Skip to content

eyas-ranjous/sort-algorithms-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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