🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

sort-algorithms-js

Package Overview
Dependencies
Maintainers
0
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-algorithms-js

production-ready sort algorithms implementation in javascript.

3.11.0
latest
Source
npm
Version published
Weekly downloads
99
33.78%
Maintainers
0
Weekly downloads
 
Created
Source

sort-algorithms-js

npm npm

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.

Table of Contents

Installation

npm install --save sort-algorithms-js

Usage

require

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

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 ]

API

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

  • Time Complexity: O(n²)
bubbleSort([2, 1, 7, 3, 9, -1, -5]);
bubbleSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);

Benchmark (Node v14)

input sizebest timeworst time
1k0s 5ms0s 9ms
10k0s 227ms0s 249ms
50k6s 411ms7s 998ms
100k26s 653ms29s 735ms
1M

insertionSort

  • Time Complexity: O(n²)
insertionSort([2, 1, 7, 3, 9, -1, -5]);
insertionSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);

Benchmark (Node v14)

input sizebest timeworst time
1k0s 5ms0s 10ms
10k0s 129ms0s 145ms
50k3s 49ms3s 596ms
100k13s 575ms16s 876ms
1M

selectionSort

  • Time Complexity: O(n²)
selectionSort([2, 1, 7, 3, 9, -1, -5]);
selectionSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);

Benchmark (Node v14)

input sizebest timeworst time
1k0s 4ms0s 8ms
10k0s 125ms0s 139ms
50k2s 178ms2s 302ms
100k9s 740ms10s 460ms
1M

radixSort

Only sorts numeric data.

  • Time Complexity: O(n × d)
// 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 sizebest timeworst time
10k0s 21ms0s 30ms
50k0s 61ms0s 81ms
100k0s 97ms0s 115ms
1M1s 27ms1s 103ms
10M13s 844ms17s 257ms
50M

heapSort

  • Time Complexity: O(n log n)
heapSort([2, 1, 7, 3, 9, -1, -5]);
heapSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);

Benchmark (Node v14)

input sizebest timeworst time
10k0s 12ms0s 14ms
50k0s 21ms0s 25ms
100k0s 31ms0s 44ms
1M0s 283ms0s 313ms
10M5s 219ms6s 367ms
50M34s 21ms46s 167ms
100M76s 485ms87s 991ms

mergeSort

  • Time Complexity: O(n log n)
mergeSort([2, 1, 7, 3, 9, -1, -5]);
mergeSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);

Benchmark (Node v14)

input sizebest timeworst time
10k0s 16ms0s 23ms
50k0s 38ms0s 45ms
100k0s 54ms0s 60ms
1M0s 413ms0s 435ms
10M5s 78ms6s 712ms
50M33s 229ms35s 659ms
100M82s 777ms86s 194ms

quickSort

  • Time Complexity: O(n log n) (average)
quickSort([2, 1, 7, 3, 9, -1, -5]);
quickSort([2, 1, 7, 3, 9, -1, -5], (a, b) => b - a);

Benchmark (Node v14)

input sizebest timeworst time
10k0s 6ms0s 13ms
50k0s 18ms0s 26ms
100k0s 26ms0s 34ms
1M0s 167ms0s 187ms
10M1s 831ms2s 188ms
50M10s 402ms14s 777ms
100M24s 253ms34s 705ms

Build

grunt build

Benchmarking

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.

License

MIT License

Keywords

sort algorithms

FAQs

Package last updated on 25 Jan 2025

Did you know?

Socket

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.

Install

Related posts