-
Notifications
You must be signed in to change notification settings - Fork 5
rangedPartialSort$
Subhajit Sahu edited this page May 3, 2023
·
1 revision
Partially arrange a range of values in order!
Alternatives: rangedPartialSort, rangedPartialSort$.
Similar: sort, rangedSort, partialSort, rangedPartialSort.
function rangedPartialSort$(x, i, I, n, fc, fm, fs)
// x: an array (updated!)
// i: begin index
// I: end index (exclusive)
// n: minimum number of values to sort
// fc: compare function (a, b)
// fm: map function (v, i, x)
// fs: swap function (x, i, j)
const xarray = require('extra-array');
// Sort a range of values (index 0 to 3)!
var x = [4, -3, 1, -2];
xarray.rangedPartialSort$(x, 0, 3, 3);
// → [ -3, 1, 4, -2 ] (compares numbers, x is updated!)
// Partially sort a range of values (index 0 to 3)!
// First 2 values are guaranteed to be sorted!
var x = [4, -3, 1, -2];
xarray.rangedPartialSort$(x, 0, 3, 2);
// → [ -3, 1, 4, -2 ] (x is updated!)
// Partially sort using absolute values!
var x = [4, -3, 1, -2];
xarray.rangedPartialSort$(x, 0, 3, 2, null, v => Math.abs(v));
// → [ 1, -3, 4, -2 ]
// Partially sort two arrays together!
var x = ['b', 'a', 'd', 'c'];
var y = [2, 1, 4, 3];
xarray.rangedPartialSort$(x, 0, 3, 2, null, null, (x, i, j) => {
xarray.swap$(x, i, j);
xarray.swap$(y, i, j);
});
x;
// → [ 'a', 'b', 'c', 'd' ]
y;
// → [ 1, 2, 3, 4 ]