Mergesort with complexity O(n log n).
npm install divide-et-impera
var mergesort = require('divide-et-impera');
// Sort an array
mergesort([3, 1, 2, 4, 2]); // => [1, 2, 2, 3, 4]
var unsorted = [3, -2, 5];
var sorted = mergesort(unsorted);
// unsorted => [3, -2, 5]
// sorted => [-2, 3, 5]
// Use a custom comparator
function desc(a, b) {
return b - a;
}
mergesort([3, 1, 2, 4, 2], desc); // => [4, 3, 2, 2, 1]