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

@omts/merge-sort

v0.0.1

Published

A TypeScript-based, flexible, and generic implementation of the **Merge Sort** algorithm. This package supports sorting arrays of various types and allows for custom comparison functions to control sorting order (ascending or descending).

Downloads

82

Readme

@omts/merge-sort 🚀

A TypeScript-based, flexible, and generic implementation of the Merge Sort algorithm. This package supports sorting arrays of various types and allows for custom comparison functions to control sorting order (ascending or descending).

Installation 📦

You can install the package via npm or pnpm:

npm install @omts/merge-sort

or

pnpm add @omts/merge-sort

Usage ✨

The package exports a mergeSort function that supports sorting any array. You can provide a custom comparison function to define the sorting behavior, such as ascending or descending order.

Example

Basic Usage

import { mergeSort } from '@omts/merge-sort';

// Sorting an array of numbers in ascending order (default)
const resultAsc = mergeSort([10, 5, 15, 1]);
console.log(resultAsc); // Output: [1, 5, 10, 15]

// Sorting an array of numbers in descending order (using custom comparator)
const resultDesc = mergeSort([10, 5, 15, 1], (a, b) => b - a);
console.log(resultDesc); // Output: [15, 10, 5, 1]

Custom Comparator

You can sort arrays of objects or use custom sorting logic by providing a custom comparator function.

interface Person {
    name: string;
    age: number;
}

const people: Person[] = [
    { name: 'Alice', age: 25 },
    { name: 'Bob', age: 20 },
    { name: 'Charlie', age: 30 }
];

// Sorting by age in ascending order
const sortedByAgeAsc = mergeSort(people, (a, b) => a.age - b.age);
console.log(sortedByAgeAsc);
/*
Output:
[
  { name: 'Bob', age: 20 },
  { name: 'Alice', age: 25 },
  { name: 'Charlie', age: 30 }
]
*/

// Sorting by age in descending order
const sortedByAgeDesc = mergeSort(people, (a, b) => b.age - a.age);
console.log(sortedByAgeDesc);
/*
Output:
[
  { name: 'Charlie', age: 30 },
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 20 }
]
*/

API Documentation 📚

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

  • Parameters:

    • arr: The array to be sorted.
    • compareFn: Optional. A custom comparison function used to compare elements. If not provided, elements are compared using the default < and > operators for ascending order.
  • Returns: A new sorted array.

Features

  • Flexible Sorting: Sorts arrays in any order based on the provided comparator function.
  • Custom Comparator: Supports custom comparison logic, useful for sorting complex data types.
  • TypeScript Support: Fully typed, ensuring type safety when sorting arrays.
  • Stable Sort: Maintains the relative order of elements with equal keys.

Time Complexity 📊

  • Time Complexity: O(n log n), where n is the number of elements in the array.
  • Space Complexity: O(n), due to the auxiliary space required for merging.

Development 🛠️

Install dependencies:

pnpm install

Run tests:

pnpm run test

Build the project:

pnpm run build

License ⚖️

This project is licensed under the MIT License - see the LICENSE file for details.


Happy sorting with @omts/merge-sort! 🎉

Feel free to use, contribute, and star the repository if you find it useful!