
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
sort-algorithms-js
Advanced tools
production-ready sort algorithms implementation in javascript.
A lightweight JavaScript library that provides multiple sorting algorithms with a unified API. You can optionally pass a compare callback similar to JavaScript’s native Array.sort
.
npm install --save sort-algorithms-js
const {
bubbleSort,
insertionSort,
selectionSort,
radixSort,
mergeSort,
heapSort,
quickSort
} = require('sort-algorithms-js');
const arr = [2, 1, 7, 3, 9, -1, -5];
console.log(bubbleSort(arr)); // [ -5, -1, 1, 2, 3, 7, 9 ]
// with a compare function for descending
console.log(insertionSort(arr, (a, b) => b - a)); // [ 9, 7, 3, 2, 1, -1, -5 ]
import {
bubbleSort,
insertionSort,
selectionSort,
radixSort,
mergeSort,
heapSort,
quickSort
} from 'sort-algorithms-js';
const arr = [2, 1, 7, 3, 9, -1, -5];
console.log(quickSort(arr)); // [ -5, -1, 1, 2, 3, 7, 9 ]
Each function sorts an array in-place and returns the same array.
Signature:
function algorithmName<T>(
list: T[],
compare?: (a: T, b: T) => number
): T[];
list
: The array to sort.compare
: An optional function that returns >0
if a>b
, <0
if a<b
, or 0
if equal.bubbleSort([2, 1, 7, 3, 9, -1, -5]);
bubbleSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
1k | 0s 5ms | 0s 9ms |
10k | 0s 227ms | 0s 249ms |
50k | 6s 411ms | 7s 998ms |
100k | 26s 653ms | 29s 735ms |
1M | ❌ | ❌ |
insertionSort([2, 1, 7, 3, 9, -1, -5]);
insertionSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
1k | 0s 5ms | 0s 10ms |
10k | 0s 129ms | 0s 145ms |
50k | 3s 49ms | 3s 596ms |
100k | 13s 575ms | 16s 876ms |
1M | ❌ | ❌ |
selectionSort([2, 1, 7, 3, 9, -1, -5]);
selectionSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
1k | 0s 4ms | 0s 8ms |
10k | 0s 125ms | 0s 139ms |
50k | 2s 178ms | 2s 302ms |
100k | 9s 740ms | 10s 460ms |
1M | ❌ | ❌ |
Only sorts numeric data.
// ascending
radixSort([2, 1, 7, 3, 9, -1, -5]);
// descending
radixSort([2, 1, 7, 3, 9, -1, -5], 'desc');
// custom numeric extraction
radixSort([{ id: 341 }, { id: 947 }, { id: 132 }], 'asc', (obj) => obj.id);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
10k | 0s 21ms | 0s 30ms |
50k | 0s 61ms | 0s 81ms |
100k | 0s 97ms | 0s 115ms |
1M | 1s 27ms | 1s 103ms |
10M | 13s 844ms | 17s 257ms |
50M | ❌ | ❌ |
heapSort([2, 1, 7, 3, 9, -1, -5]);
heapSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
10k | 0s 12ms | 0s 14ms |
50k | 0s 21ms | 0s 25ms |
100k | 0s 31ms | 0s 44ms |
1M | 0s 283ms | 0s 313ms |
10M | 5s 219ms | 6s 367ms |
50M | 34s 21ms | 46s 167ms |
100M | 76s 485ms | 87s 991ms |
mergeSort([2, 1, 7, 3, 9, -1, -5]);
mergeSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
10k | 0s 16ms | 0s 23ms |
50k | 0s 38ms | 0s 45ms |
100k | 0s 54ms | 0s 60ms |
1M | 0s 413ms | 0s 435ms |
10M | 5s 78ms | 6s 712ms |
50M | 33s 229ms | 35s 659ms |
100M | 82s 777ms | 86s 194ms |
quickSort([2, 1, 7, 3, 9, -1, -5]);
quickSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);
Benchmark (Node v14)
input size | best time | worst time |
---|---|---|
10k | 0s 6ms | 0s 13ms |
50k | 0s 18ms | 0s 26ms |
100k | 0s 26ms | 0s 34ms |
1M | 0s 167ms | 0s 187ms |
10M | 1s 831ms | 2s 188ms |
50M | 10s 402ms | 14s 777ms |
100M | 24s 253ms | 34s 705ms |
grunt build
If you’d like to benchmark a specific sorting algorithm on random data, use the included benchmark.js
script:
node test/benchmark.js \
--size 100000 \
--algorithm heapSort \
--iterations 5
size
: The size of the randomly generated array.algorithm
: One of the supported algorithms (e.g., bubbleSort
, quickSort
, heapSort
, etc.).iterations
(optional): How many times to repeat the test.Example Output:
heapSort: 0 seconds 51 ms
heapSort: 0 seconds 44 ms
heapSort: 0 seconds 52 ms
This script creates a random array of the specified size, times the sorting operation (using timer-node), and repeats that process for a given number of iterations. It’s a quick way to compare the relative performance of different sorting algorithms on your machine.
[3.11.0] - 2025-01-25
FAQs
production-ready sort algorithms implementation in javascript.
The npm package sort-algorithms-js receives a total of 78 weekly downloads. As such, sort-algorithms-js popularity was classified as not popular.
We found that sort-algorithms-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.