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

unshuffle

v1.0.0

Published

In-place sort of linked lists

Downloads

1

Readme

Unshuffle

This library provides an implementation of the Unshuffle Sort Algorithm for linked lists. This is an in-place sort that uses a worst-case O(n/2) amount of scratch space to re-configure the links in a singly-linked list to put the elements in sorted order. It is designed to efficiently exploit existing runs of ordered or anti-ordered elements, having a time complexity of O(kn), where k is a constant proportional to the amount of entropy in the input data. For totally random data, k approaches log(n).

The module exports a single unshuffle function with the following signatures:

  • function unshuffle<C extends { value: number }, L extends C>(head: L, cmp?: null, ptr?: keyof L): L
  • function unshuffle<C, L extends C>(head: L, cmp: Compare<C>, ptr?: keyof L): L

where Compare<C> is an alias for <C>(a: C, b: C) => number--i.e., a generic comparison function that takes two objects of the same type and returns a negative number is a < b, zero if a = b, and a positive number if a > b;

The type L represents a generic linked list node--i.e., an object that has a field pointing to another object conforming to the same interface--which must also be comparable by the given comparator function. If no comparator function is supplied (it is left out, or null or undefined are passed in), the default comparator requires L objects to have a numeric value field. Additionally, by default unshuffle will require that L objects are linked by a next field pointing to another L object or null; however, that can be overwritten by providing the optional ptr argument to specify an alternate name for the link-pointer field, which must be a key of whatever the L type actually is. (Unfortunately, the TypeScript type system is not quite powerful enough to statically guarantee that custom pointer fields actually recursively point to additional L values; if you use custom pointer fields, you are on your own to ensure that they are properly typed.)

Examples

const { unshuffle } = require('unshuffle');

function randLinkedList(n, key){
  // Note that we are using the default 'value' field with numeric values,
  // so we won't need to pass in a custom comparison function for sorting.
  const head = { value: Math.round(Math.random() * 1000), [key]: null };
  let node = head;
  for (;n>0;n--) {
    node = node[key] = { value: Math.round(Math.random() * 1000), [key]: null };
  }
  return head;
}

let unsortedHead = randLinkedList(100, 'next');
let sortedHead = unshuffle(unsortedHead);
// sortedHead is an element of the list originally headed by,
// unsortedHead, which will now appear somewhere in the middle
// of the list because links were re-arranged in-place.

// let's try a list with a different link pointer field
unsortedHead = randLinkedList(100, 'foo');
sortedHead = unshuffle(unsortedHead, null /* still using the default comparator */, 'foo');