aureooms/js-radix-sort

View on GitHub
src/array/api/sort.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import assert from 'assert';

import sortArbitraryTuples from '../core/sortArbitraryTuples.js';
import sortFixedLengthTuples from '../core/sortFixedLengthTuples.js';

const sort = (k, M, tuples) => {
    assert(Number.isInteger(k));
    assert(Number.isInteger(M));
    console.debug('sort', k, tuples);
    if (k === 0) {
        return tuples;
    }

    const N = tuples.length;
    if (N <= 1) {
        return tuples;
    }

    // eslint-disable-next-line unicorn/no-new-array
    const output = new Array(N);
    if (k >= 1) {
        return sortFixedLengthTuples(k, M, tuples, output, 0, N);
    }

    return sortArbitraryTuples(M, tuples, output);
};

export default sort;