cocktailsort
Version:
Cocktail Sort (bidirectional bubble sort)
33 lines (22 loc) • 1.13 kB
Markdown
> Cocktail Sort (bidirectional bubble sort) extends bubble sort by operating in two directions. It improves on bubble sort by more quickly moving items to the beginning of the list.
<a href="https://npmjs.com/package/cocktailsort"><img alt="NPM" src="https://img.shields.io/npm/v/cocktailsort" /></a>
<a href="https://github.com/coderosh/cocktailsort"><img alt="MIT" src="https://img.shields.io/badge/license-MIT-blue.svg" /></a>
<a href="#"><img alt="CI" src="https://img.shields.io/github/workflow/status/coderosh/cocktailsort/CI"></a>
<a href="https://github.com/coderosh/cocktailsort"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PRs welcome!" /></a>
```sh
npm install cocktailsort
yarn add cocktailsort
```
```js
import sort from 'cocktailsort'
// ascending
sort([4, 2, 0, -1, 9]) // [-1, 0, 2, 4, 9]
// descending
sort([4, 2, 0, -1, 9], (a, b) => b - a) // [9, 5, 2, 0, -1]
// array of objects as input
sort([{ i: 10 }, { i: 2 }, { i: 9 }], (a, b) => a.i - b.i) // [{i: 2}, {i: 9}, {i: 10}]
```