From 4537784f3bffe804856bc5e00428289450d1c259 Mon Sep 17 00:00:00 2001 From: YAKSHITH-1 Date: Tue, 1 Dec 2026 20:32:42 +0530 Subject: [PATCH 1/2] feat: add PowerSort algorithm Fixes: #2042 --- Sorts/PowerSort.js | 205 +++++++++++++++++++++++++++++++++++ Sorts/test/PowerSort.test.js | 83 ++++++++++++++ 2 files changed, 288 insertions(+) create mode 100644 Sorts/PowerSort.js create mode 100644 Sorts/test/PowerSort.test.js diff --git a/Sorts/PowerSort.js b/Sorts/PowerSort.js new file mode 100644 index 0000000000..8f17a33505 --- /dev/null +++ b/Sorts/PowerSort.js @@ -0,0 +1,205 @@ +/** + * Find a natural ascending or descending run. + * + * @param {Array} array Input array. + * @param {number} start Run starting index. + * @return {number} Exclusive end index. + */ +function findRun(array, start) { + const length = array.length + + if (start >= length - 1) { + return length + } + + let end = start + 2 + + if (array[start] <= array[start + 1]) { + while (end < length && array[end - 1] <= array[end]) { + end++ + } + } else { + while (end < length && array[end - 1] > array[end]) { + end++ + } + + let left = start + let right = end - 1 + + while (left < right) { + ;[array[left], array[right]] = [array[right], array[left]] + left++ + right-- + } + } + + return end +} + +/** + * Extend a run using binary insertion sort. + * + * @param {Array} array Input array. + * @param {number} start Run starting index. + * @param {number} end Exclusive end index. + */ +function binaryInsertionSort(array, start, end) { + for (let i = start + 1; i < end; i++) { + const pivot = array[i] + + let left = start + let right = i + + while (left < right) { + const mid = left + Math.floor((right - left) / 2) + + if (pivot < array[mid]) { + right = mid + } else { + left = mid + 1 + } + } + + for (let j = i; j > left; j--) { + array[j] = array[j - 1] + } + + array[left] = pivot + } +} + +/** + * Calculate the power of the boundary between two runs. + * + * @param {number} start First run start. + * @param {number} middle Second run start. + * @param {number} end Second run end. + * @param {number} length Array length. + * @return {number} Node power. + */ +function nodePower(start, middle, end, length) { + const denominator = 2 * length + + let leftRemainder = start + middle + let rightRemainder = middle + end + let power = 0 + + while (true) { + const leftBit = leftRemainder * 2 >= denominator ? 1 : 0 + const rightBit = rightRemainder * 2 >= denominator ? 1 : 0 + + if (leftBit !== rightBit) { + return power + 1 + } + + leftRemainder = (leftRemainder * 2) % denominator + rightRemainder = (rightRemainder * 2) % denominator + + power++ + } +} + +/** + * Merge two adjacent sorted runs. + * + * @param {Array} array Input array. + * @param {number} start First run start. + * @param {number} middle Second run start. + * @param {number} end Second run end. + */ +function merge(array, start, middle, end) { + const left = array.slice(start, middle) + const right = array.slice(middle, end) + + let i = 0 + let j = 0 + let k = start + + while (i < left.length && j < right.length) { + if (left[i] <= right[j]) { + array[k++] = left[i++] + } else { + array[k++] = right[j++] + } + } + + while (i < left.length) { + array[k++] = left[i++] + } + + while (j < right.length) { + array[k++] = right[j++] + } +} + +/** + * Sort an array using PowerSort. + * + * Reference: https://www.wild-inter.net/publications/munro-wild-2018 + * + * @param {Array} array Array to sort. + * @return {Array} Sorted array. + */ +function powerSort(array) { + const length = array.length + const MIN_RUN = 32 + + if (length < 2) { + return array + } + + function getRunEnd(start) { + let end = findRun(array, start) + const minRunEnd = Math.min(start + MIN_RUN, length) + + if (end < minRunEnd) { + binaryInsertionSort(array, start, minRunEnd) + end = minRunEnd + } + + return end + } + + const pendingRuns = [] + const powers = [] + + let currentStart = 0 + let currentEnd = getRunEnd(currentStart) + + while (currentEnd < length) { + const nextStart = currentEnd + const nextEnd = getRunEnd(nextStart) + + const power = nodePower(currentStart, nextStart, nextEnd, length) + + // Merge when the previous boundary has greater power. + while (powers.length > 0 && powers[powers.length - 1] > power) { + powers.pop() + + const leftStart = pendingRuns.pop() + + merge(array, leftStart, currentStart, currentEnd) + + currentStart = leftStart + } + + pendingRuns.push(currentStart) + + powers.push(power) + + currentStart = nextStart + currentEnd = nextEnd + } + + while (pendingRuns.length > 0) { + const leftStart = pendingRuns.pop() + + merge(array, leftStart, currentStart, currentEnd) + + currentStart = leftStart + } + + return array +} + +export { powerSort } diff --git a/Sorts/test/PowerSort.test.js b/Sorts/test/PowerSort.test.js new file mode 100644 index 0000000000..77340bb0cf --- /dev/null +++ b/Sorts/test/PowerSort.test.js @@ -0,0 +1,83 @@ +import { powerSort } from '../PowerSort' + +describe('PowerSort', () => { + it('should work for empty arrays', () => { + expect(powerSort([])).toEqual([]) + }) + + it('should work for single-element arrays', () => { + expect(powerSort([5])).toEqual([5]) + }) + + it('should sort arrays correctly', () => { + expect(powerSort([5, 4])).toEqual([4, 5]) + + expect(powerSort([8, 4, 10, 15, 9])).toEqual([4, 8, 9, 10, 15]) + + expect(powerSort([1, 2, 3])).toEqual([1, 2, 3]) + + expect(powerSort([10, 5, 3, 8, 2, 6, 4, 7, 9, 1])).toEqual([ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 + ]) + }) + + it('should sort arrays containing duplicate values', () => { + expect(powerSort([4, 2, 4, 1, 2, 3, 1])).toEqual([1, 1, 2, 2, 3, 4, 4]) + }) + + it('should handle already sorted arrays', () => { + expect(powerSort([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]) + }) + + it('should handle reverse-sorted arrays', () => { + expect(powerSort([9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([ + 1, 2, 3, 4, 5, 6, 7, 8, 9 + ]) + }) + + it('should sort arrays larger than MIN_RUN', () => { + const array = Array.from({ length: 100 }, (_, index) => 100 - index) + + expect(powerSort(array)).toEqual( + Array.from({ length: 100 }, (_, index) => index + 1) + ) + }) + + it('should return the same array after sorting', () => { + const array = [5, 3, 1, 4, 2] + + const result = powerSort(array) + + expect(result).toBe(array) + expect(array).toEqual([1, 2, 3, 4, 5]) + }) + + it('should merge multiple natural runs correctly', () => { + const array = [ + ...Array.from({ length: 32 }, (_, i) => i + 96), + ...Array.from({ length: 32 }, (_, i) => i + 64), + ...Array.from({ length: 32 }, (_, i) => i + 32), + ...Array.from({ length: 32 }, (_, i) => i) + ] + + expect(powerSort(array)).toEqual(Array.from({ length: 128 }, (_, i) => i)) + }) + + it('should merge natural runs of different lengths', () => { + const array = [ + ...Array.from({ length: 32 }, (_, i) => i + 200), + ...Array.from({ length: 40 }, (_, i) => i + 150), + ...Array.from({ length: 35 }, (_, i) => i + 100), + ...Array.from({ length: 50 }, (_, i) => i) + ] + + const expected = [ + ...Array.from({ length: 50 }, (_, i) => i), + ...Array.from({ length: 35 }, (_, i) => i + 100), + ...Array.from({ length: 40 }, (_, i) => i + 150), + ...Array.from({ length: 32 }, (_, i) => i + 200) + ] + + expect(powerSort(array)).toEqual(expected) + }) +}) From 4dff71e01be01f6162147d1ca9eb647ab5998d6c Mon Sep 17 00:00:00 2001 From: YAKSHITH-1 Date: Tue, 1 Dec 2026 21:00:46 +0530 Subject: [PATCH 2/2] test: improve PowerSort coverage --- Sorts/test/PowerSort.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sorts/test/PowerSort.test.js b/Sorts/test/PowerSort.test.js index 77340bb0cf..9f257d605d 100644 --- a/Sorts/test/PowerSort.test.js +++ b/Sorts/test/PowerSort.test.js @@ -80,4 +80,15 @@ describe('PowerSort', () => { expect(powerSort(array)).toEqual(expected) }) + it('should handle a final single-element run', () => { + const array = [ + ...Array.from({ length: 32 }, (_, index) => 100 - index), + 200 + ] + + expect(powerSort(array)).toEqual([ + ...Array.from({ length: 32 }, (_, index) => index + 69), + 200 + ]) + }) })