# steinhaus-johnson-trotter

> Generate permutations using the Steinhaus-Johnson-Trotter algorithm.

Latest version **1.1.0** (published 2015-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install steinhaus-johnson-trotter
pnpm add steinhaus-johnson-trotter
yarn add steinhaus-johnson-trotter
bun add steinhaus-johnson-trotter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2015-10-13 |
| First published | 2015-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Julian Alexander Fleischer |
| Maintainers | scravy |
| Keywords | permutations, algorithm, steinhaus, johnson, trotter, even |

## Links

- npm: https://www.npmjs.com/package/steinhaus-johnson-trotter
- Repository: https://github.com/nodash/steinhaus-johnson-trotter
- Issues: https://github.com/nodash/steinhaus-johnson-trotter/issues
- npm.io page: https://npm.io/package/steinhaus-johnson-trotter

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2015-10-13
- 1.0.0 — 2015-10-12

## README

steinhaus-johnson-trotter
=========================

A JavaScript implementation of the
[Steinhaus-Johnson-Trotter algorithm with Even's speedup](https://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm)
to generate the permutations of a string or an array.

Usage
-----

```JavaScript
var permutations = require('steinhaus-johnson-trotter');

var generate = permutations("123");

console.log(generate()); // → '132'
console.log(generate()); // → '312'
console.log(generate()); // → '321'
console.log(generate()); // → '231'
console.log(generate()); // → '213'
console.log(generate()); // → undefined
```

`permutations` returns a function which returns another
permutation each time is is invoked. If all permutations
are generated it returns `undefined`. The source is never
included in the permutations returned, i.e. the number of
invocations that the generator returns a permutation is
`N! - 1` where N is the length of the array/string.

All permutations can be generated as follows:

```JavaScript
var sjt = require('steinhaus-johnson-trotter');

function permutations(arr) {
  var generator = sjt(arr);
  var next = arr;
  var result = [];
  while (next !== undefined) {
    result.push(next);
    next = generator();
  }
  return result;
}
```

The above function is also exported as `all`:

```JavaScript
var permutations = require('steinhaus-johnson-trotter');

console.log(permutations.all([ 1, 4, 7 ]));

/* → [ [ 1, 4, 7 ],
       [ 1, 7, 4 ],
       [ 7, 1, 4 ],
       [ 7, 4, 1 ],
       [ 4, 7, 1 ],
       [ 4, 1, 7 ] ] */
```

---
_Source: https://npm.io/package/steinhaus-johnson-trotter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
