rough-sortedness
v1.0.1
Published
Utility to compute the rough sortedness of a list
Downloads
27
Readme
rough-sortedness
Utility to compute the rough sortedness of a list.
Rough-Sortedness takes the ratio of the number of incrementing sequences in an array and divides it by the length of the total array to estimate the general sortedness of the array.
Warning: this rough sortedness metric is blind to interleaved sorted sequences, and reversed but perfectly sorted arrays will return a score of 0.
Usage
const roughSortedness = require('rough-sortedness')
var sorted = [1,2,3,4,5,6]
var unsorted = [1,2,3,4,6,5]
console.log(roughSortedness(sorted) > roughSortedness(unsorted))
API
roughSortedness
roughSortedness(arr, (a,b)=>{return a.value - b.value})
The function expects an array of items, and a comparator function. If no comparator function is provided, it will default to function defaultComparator (a, b) {return a - b}
. The function returns a value from 0 to 1, where 1 is perfectly sorted, and 0 is impossibly unsorted.
Installation
$ npm install rough-sortedness