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

sortire

v1.1.0

Published

A collection of popular sorting algorithms for JavaScript

Downloads

38

Readme

Sortire

Minified size Test coverage Monthly download

Sortire is a library that provides a variety of sorting algorithms for use in JavaScript. It includes implementations of popular algorithms such as bubble sort, insertion sort, selection sort, heap sort, merge sort, and quick sort. These algorithms can be customized with a comparison function to specify the order of the elements in the array. All of the functions in Sortire return a shallow copy of the modified array.

Features

Installation

To install sortire, run the following command:

NPM

npm install sortire

Yarn

yarn add sortire

pnpm

pnpm add sortire

Basic Usage

import { bubbleSort } from "sortire";
// import { bubbleSort } from "npm:sortire"; // (for Deno)

const array = [5, 3, 8, 4, 9, 1, 6, 2, 7];

// Sort in ascending order
const sortedAsc = bubbleSort(array, (a, b) => a - b);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

// Sort in descending order
const sortedDesc = bubbleSort(array, (a, b) => b - a);
// [9, 8, 7, 6, 5, 4, 3, 2, 1]

Strings

import { bubbleSort } from "sortire";
// import { bubbleSort } from "npm:sortire"; // (for Deno)

const array = ["Red", "Green", "Blue", "Yellow", "Purple"];

// Sort in ascending order
const sortedAsc = bubbleSort(array, (a, b) => (a.name < b.name ? -1 : 1));
// ['Blue', 'Green', 'Purple', 'Red', 'Yellow']

// Sort in descending order
const sortedDesc = bubbleSort(array, (a, b) => (a.name > b.name ? -1 : 1));
// ['Yellow', 'Red', 'Purple', 'Green', 'Blue']

Array of Objects

import { bubbleSort } from "sortire";
// import { bubbleSort } from "npm:sortire"; // (for Deno)

const array = [
  { id: 1, name: "Red" },
  { id: 2, name: "Green" },
  { id: 3, name: "Blue" },
  { id: 4, name: "Yellow" },
  { id: 5, name: "Purple" },
];

// Sort by name in ascending order
const sortedAsc = bubbleSort(array, (a, b) => (a.name < b.name ? -1 : 1));
// [
//   { id: 3, name: 'Blue' },
//   { id: 2, name: 'Green' },
//   { id: 5, name: 'Purple' },
//   { id: 1, name: 'Red' },
//   { id: 4, name: 'Yellow' },
// ]

// Sort by name in descending order
const sortedDesc = bubbleSort(array, (a, b) => (a.name > b.name ? -1 : 1));
// [
//   { id: 4, name: 'Yellow' },
//   { id: 1, name: 'Red' },
//   { id: 5, name: 'Purple' },
//   { id: 2, name: 'Green' },
//   { id: 3, name: 'Blue' },
// ]

Small Range Arrays

For small range arrays, bubble sort, insertion sort, and selection sort are all excellent options. These algorithms have a time complexity of O(n^2), which means that the time it takes to sort the array increases at a faster rate as the size of the array increases. While they may not be as efficient as O(n log n) algorithms for large arrays, they are still very effective for sorting small arrays, as the sorting time will be relatively quick even for very small input sizes.

Bubble sort

Bubble sort is a simple sorting algorithm that works by repeatedly iterating through the array and swapping adjacent elements if they are in the wrong order.

Syntax

bubbleSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]

Time complexity

O(n^2), where n is the length of the array.

Insertion sort

Insertion sort works by repeatedly inserting the next element in the correct position in the sorted portion of the array.

Syntax

insertionSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]

Time complexity

O(n^2), where n is the length of the array.

Selection sort

Selection sort works by repeatedly selecting the smallest element in the unsorted portion of the array and swapping it with the first element in the unsorted portion.

Syntax

selectionSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]

Time complexity

O(n^2), where n is the length of the array.

Large Range Arrays

For large range arrays, heap sort, merge sort, and quick sort are all good choices. These algorithms have a time complexity of O(n log n), which means that the time it takes to sort the array increases at a slower rate as the size of the array increases. This makes them well-suited for sorting large arrays, as the sorting time will not become excessively long even for very large input sizes.

Heap sort

Heap sort is a sorting algorithm that works by building a heap data structure from the input array and repeatedly extracting the maximum element from the heap. It requires additional space for the heap data structure, which can make it less suitable for large arrays that do not fit in memory.

Syntax

heapSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]

Time complexity

O(n log n), where n is the length of the array.

Merge sort

Merge sort is a sorting algorithm that works by dividing the input array in half, sorting each half, and then merging the sorted halves back together.

Syntax

mergeSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]

Time complexity

O(n log n), where n is the length of the array.

Quick sort

Quick sort is a sorting algorithm that works by selecting a pivot element from the array and partitioning the array into two halves around the pivot. It can be less efficient for arrays with a large number of duplicate elements or for arrays that are already partially sorted.

Syntax

quickSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]

Time complexity

O(n log n), where n is the length of the array.

License

MIT License