1.0.2 • Published 7 years ago

is-single-swap-enough v1.0.2

Weekly downloads
1
License
WTFPL
Repository
github
Last release
7 years ago

Is single swap enough to sort array?

I ask myself that everyday. And finally i can know the answer.


Problem

Given an array A of length N return true if A can be sorted using at most single element swap. Return false otherwise.


Install

npm install is-single-swap-enough


Example

const isSingleSwapEnough = require('is-single-swap-enough');

isSingleSwapEnough([1, 3, 2, 4]); // true

isSingleSwapEnough([40, 50, 10]); // false

Documentation

isSingleSwapEnough

isSingleSwapEnough(array, [comparator], [isSortingDescending]) => boolean

Arguments

array

Array of any items. If compare function is not passed, then assumed, that this is number[]

comparator

Optional. Function that does compare items in array. For more info go to MDN

isSortingDescending

Optional. Defines sort order: defaults to false.


Thanks to this