I ask myself that everyday. And finally i can know the answer.
Given an array A
of length N
return true
if A
can be sorted using at most single element swap. Return false
otherwise.
npm install is-single-swap-enough
const isSingleSwapEnough = require('is-single-swap-enough');
isSingleSwapEnough([1, 3, 2, 4]); // true
isSingleSwapEnough([40, 50, 10]); // false
isSingleSwapEnough(array, [comparator], [isSortingDescending]) => boolean
Array of any
items. If compare function is not passed, then assumed, that this is number[]
Optional. Function that does compare items in array. For more info go to MDN
Optional. Defines sort order: defaults to false
.