npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hector-sorts

v2.0.7

Published

A collection of sorting algorithms implemented in JavaScript.

Downloads

27

Readme

hector-sorts: Sorting Algorithms

This package provides a collection of common sorting algorithms implemented in JavaScript. These sorting algorithms can be used to sort arrays of numbers or any comparable elements.

Installation

You can install the package via npm:

npm i hector-sorts

Usage

const sortingAlgorithms = require("hector-sorts");

// Example usage of sorting algorithms
const arr = [5, 3, 8, 1, 2, 7, 4, 6];

// Bubble Sort
console.log("Bubble Sort:", sortingAlgorithms.bubbleSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Selection Sort
console.log("Selection Sort:", sortingAlgorithms.selectionSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Insertion Sort
console.log("Insertion Sort:", sortingAlgorithms.insertionSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Merge Sort
console.log("Merge Sort:", sortingAlgorithms.mergeSort(arr.slice()));
// Time Complexity: O(n log n)
// Space Complexity: O(n)

// Quick Sort
console.log("Quick Sort:", sortingAlgorithms.quickSort(arr.slice()));
// Time Complexity: O(n log n) average case, O(n^2) worst case
// Space Complexity: O(log n)

// Heap Sort
console.log("Heap Sort:", sortingAlgorithms.heapSort(arr.slice()));
// Time Complexity: O(n log n)
// Space Complexity: O(1)

// Counting Sort
// primarily designed for sorting arrays of numbers
console.log("Counting Sort:", sortingAlgorithms.countingSort(arr.slice()));
// Time Complexity: O(n + k) where k is the range of the input
// Space Complexity: O(n + k)

// Radix Sort
// Operates on integer keys and expects arrays of numbers for sorting. Sorting arrays of strings may lead to unexpected behavior.
console.log("Radix Sort:", sortingAlgorithms.radixSort(arr.slice()));
// Time Complexity: O(nk) where k is the number of digits in the largest number
// Space Complexity: O(n + k)

// Bucket Sort
// Can handle arrays of strings, its primary use case is sorting arrays of numbers. Sorting arrays of strings may not produce the expected results.
console.log("Bucket Sort:", sortingAlgorithms.bucketSort(arr.slice()));
// Time Complexity: O(n^2) worst case, but typically O(n + k) when k is the number of buckets
// Space Complexity: O(n + k)

// Shell Sort
console.log("Shell Sort:", sortingAlgorithms.shellSort(arr.slice()));
// Time Complexity: O(n log^2 n)
// Space Complexity: O(1)

// Cocktail Shaker Sort
console.log(
  "Cocktail Shaker Sort:",
  sortingAlgorithms.cocktailShakerSort(arr.slice())
);
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Comb Sort
console.log("Comb Sort:", sortingAlgorithms.combSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Gnome Sort
console.log("Gnome Sort:", sortingAlgorithms.gnomeSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Cycle Sort
console.log("Cycle Sort:", sortingAlgorithms.cycleSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Pancake Sort
// Pancake Sort is primarily designed for sorting arrays of numbers, not arrays of strings.
console.log("Pancake Sort:", sortingAlgorithms.pancakeSort(arr.slice()));
// Time Complexity: O(n^2)
// Space Complexity: O(1)

// Bogosort
console.log("Bogosort:", sortingAlgorithms.bogoSort(arr.slice()));
// Time Complexity: O((n+1)!)
// Space Complexity: O(1)

// Stooge Sort
console.log("Stooge Sort:", sortingAlgorithms.stoogeSort([...arr]));
// Time Complexity: O(n^(log 3 / log 1.5)) = O(n^2.7095)
// Space Complexity: O(1)

// Bitonic Sort
// Special Requirements: Bitonic sort requires the input size to be a power of 2.
console.log(
  "Bitonic Sort (Ascending):",
  sortingAlgorithms.bitonicSort([...arr])
);
console.log(
  "Bitonic Sort (Descending):",
  sortingAlgorithms.bitonicSort([...arr], false)
);
// Time Complexity: O(log^2 n)
// Space Complexity: O(n log n)

// Timsort
console.log("Timsort:", sortingAlgorithms.timSort([...arr]));
// Time Complexity: O(n log n)
// Space Complexity: O(n)

// Introsort
console.log("Introsort:", sortingAlgorithms.introSort([...arr]));
// Time Complexity: O(n log n)
// Space Complexity: O(log n)

// Strand Sort
console.log("Strand Sort:", sortingAlgorithms.strandSort([...arr]));
// Time Complexity: O(n^2)
// Space Complexity: O(n)

// Library Sort
// Special Requirements: Library sort works best for sorting numbers. It may not behave as expected for arrays containing non-numeric elements.
console.log("Library Sort:", sortingAlgorithms.librarySort([...arr]));
// Time Complexity: O(n log n)
// Space Complexity: O(n)