Skip to content

showmeyourhits/is-single-swap-enough

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Is single swap enough to sort array?

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published