bottomUpMergeSort.mjs
ytp.me·5d
📝CMS
Preview
Report Post
// bottomUpMergeSort.mjs // Stable bottom-up merge sort (ES module) /** * Bottom-up stable merge sort (iterative) with an optional comparator. * - Default comparator follows Array.prototype.sort style: return <0, 0, >0 * - Stable: preserves relative order of equal elements * - Reuses a single temporary buffer for all merges * * @param {Array} arr - Array to sort in place * @param {Function} comparator - Optional comparator (a, b) => number * @returns {Array} The sorted array (same reference as input) */ export function bottomUpMergeSort(arr, comparator = defaultComparator) { const n = arr.length; if (n <= 1) return arr; const cmp = normalizeComparator(comparator); const temp = new Array(n); for (let width = 1; width < n; width *= 2) { f...

Similar Posts

Loading similar posts...