@numio/bigmath
Version:
@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)
35 lines (34 loc) • 1.11 kB
JavaScript
import { compareInner } from "../compare/utils.js";
import { ASC } from "./constants.js";
const heapify = (array, len, i, sorting) => {
const idxL = 2 * i + 1;
const idxR = idxL + 1;
let idx = i;
if (sorting === ASC) {
if (idxL < len && compareInner(array[idxL], array[idx])[1] >= 0)
idx = idxL;
if (idxR < len && compareInner(array[idxR], array[idx])[1] >= 0)
idx = idxR;
}
else {
if (idxL < len && compareInner(array[idx], array[idxL])[1] >= 0)
idx = idxL;
if (idxR < len && compareInner(array[idx], array[idxR])[1] >= 0)
idx = idxR;
}
if (idx !== i) {
[array[i], array[idx]] = [array[idx], array[i]];
heapify(array, len, idx, sorting);
}
};
export const sortInner = (array, sorting) => {
const { length: len } = array;
for (let i = (len >> 1) - 1; i >= 0; i--) {
heapify(array, len, i, sorting);
}
for (let i = len - 1; i >= 0; i--) {
[array[0], array[i]] = [array[i], array[0]];
heapify(array, i, 0, sorting);
}
return array;
};