sort-algorithms-js
v3.10.3
Published
production-ready sort algorithms implementation in javascript.
Downloads
310
Maintainers
Readme
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
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
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
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
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
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
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
Build
grunt build
License
The MIT License. Full License is here