Skip to content

Commit 4537784

Browse files
committed
feat: add PowerSort algorithm
Fixes: #2042
1 parent 5c39e87 commit 4537784

2 files changed

Lines changed: 288 additions & 0 deletions

File tree

‎Sorts/PowerSort.js‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
* Find a natural ascending or descending run.
3+
*
4+
* @param {Array} array Input array.
5+
* @param {number} start Run starting index.
6+
* @return {number} Exclusive end index.
7+
*/
8+
function findRun(array, start) {
9+
const length = array.length
10+
11+
if (start >= length - 1) {
12+
return length
13+
}
14+
15+
let end = start + 2
16+
17+
if (array[start] <= array[start + 1]) {
18+
while (end < length && array[end - 1] <= array[end]) {
19+
end++
20+
}
21+
} else {
22+
while (end < length && array[end - 1] > array[end]) {
23+
end++
24+
}
25+
26+
let left = start
27+
let right = end - 1
28+
29+
while (left < right) {
30+
;[array[left], array[right]] = [array[right], array[left]]
31+
left++
32+
right--
33+
}
34+
}
35+
36+
return end
37+
}
38+
39+
/**
40+
* Extend a run using binary insertion sort.
41+
*
42+
* @param {Array} array Input array.
43+
* @param {number} start Run starting index.
44+
* @param {number} end Exclusive end index.
45+
*/
46+
function binaryInsertionSort(array, start, end) {
47+
for (let i = start + 1; i < end; i++) {
48+
const pivot = array[i]
49+
50+
let left = start
51+
let right = i
52+
53+
while (left < right) {
54+
const mid = left + Math.floor((right - left) / 2)
55+
56+
if (pivot < array[mid]) {
57+
right = mid
58+
} else {
59+
left = mid + 1
60+
}
61+
}
62+
63+
for (let j = i; j > left; j--) {
64+
array[j] = array[j - 1]
65+
}
66+
67+
array[left] = pivot
68+
}
69+
}
70+
71+
/**
72+
* Calculate the power of the boundary between two runs.
73+
*
74+
* @param {number} start First run start.
75+
* @param {number} middle Second run start.
76+
* @param {number} end Second run end.
77+
* @param {number} length Array length.
78+
* @return {number} Node power.
79+
*/
80+
function nodePower(start, middle, end, length) {
81+
const denominator = 2 * length
82+
83+
let leftRemainder = start + middle
84+
let rightRemainder = middle + end
85+
let power = 0
86+
87+
while (true) {
88+
const leftBit = leftRemainder * 2 >= denominator ? 1 : 0
89+
const rightBit = rightRemainder * 2 >= denominator ? 1 : 0
90+
91+
if (leftBit !== rightBit) {
92+
return power + 1
93+
}
94+
95+
leftRemainder = (leftRemainder * 2) % denominator
96+
rightRemainder = (rightRemainder * 2) % denominator
97+
98+
power++
99+
}
100+
}
101+
102+
/**
103+
* Merge two adjacent sorted runs.
104+
*
105+
* @param {Array} array Input array.
106+
* @param {number} start First run start.
107+
* @param {number} middle Second run start.
108+
* @param {number} end Second run end.
109+
*/
110+
function merge(array, start, middle, end) {
111+
const left = array.slice(start, middle)
112+
const right = array.slice(middle, end)
113+
114+
let i = 0
115+
let j = 0
116+
let k = start
117+
118+
while (i < left.length && j < right.length) {
119+
if (left[i] <= right[j]) {
120+
array[k++] = left[i++]
121+
} else {
122+
array[k++] = right[j++]
123+
}
124+
}
125+
126+
while (i < left.length) {
127+
array[k++] = left[i++]
128+
}
129+
130+
while (j < right.length) {
131+
array[k++] = right[j++]
132+
}
133+
}
134+
135+
/**
136+
* Sort an array using PowerSort.
137+
*
138+
* Reference: https://www.wild-inter.net/publications/munro-wild-2018
139+
*
140+
* @param {Array} array Array to sort.
141+
* @return {Array} Sorted array.
142+
*/
143+
function powerSort(array) {
144+
const length = array.length
145+
const MIN_RUN = 32
146+
147+
if (length < 2) {
148+
return array
149+
}
150+
151+
function getRunEnd(start) {
152+
let end = findRun(array, start)
153+
const minRunEnd = Math.min(start + MIN_RUN, length)
154+
155+
if (end < minRunEnd) {
156+
binaryInsertionSort(array, start, minRunEnd)
157+
end = minRunEnd
158+
}
159+
160+
return end
161+
}
162+
163+
const pendingRuns = []
164+
const powers = []
165+
166+
let currentStart = 0
167+
let currentEnd = getRunEnd(currentStart)
168+
169+
while (currentEnd < length) {
170+
const nextStart = currentEnd
171+
const nextEnd = getRunEnd(nextStart)
172+
173+
const power = nodePower(currentStart, nextStart, nextEnd, length)
174+
175+
// Merge when the previous boundary has greater power.
176+
while (powers.length > 0 && powers[powers.length - 1] > power) {
177+
powers.pop()
178+
179+
const leftStart = pendingRuns.pop()
180+
181+
merge(array, leftStart, currentStart, currentEnd)
182+
183+
currentStart = leftStart
184+
}
185+
186+
pendingRuns.push(currentStart)
187+
188+
powers.push(power)
189+
190+
currentStart = nextStart
191+
currentEnd = nextEnd
192+
}
193+
194+
while (pendingRuns.length > 0) {
195+
const leftStart = pendingRuns.pop()
196+
197+
merge(array, leftStart, currentStart, currentEnd)
198+
199+
currentStart = leftStart
200+
}
201+
202+
return array
203+
}
204+
205+
export { powerSort }

‎Sorts/test/PowerSort.test.js‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { powerSort } from '../PowerSort'
2+
3+
describe('PowerSort', () => {
4+
it('should work for empty arrays', () => {
5+
expect(powerSort([])).toEqual([])
6+
})
7+
8+
it('should work for single-element arrays', () => {
9+
expect(powerSort([5])).toEqual([5])
10+
})
11+
12+
it('should sort arrays correctly', () => {
13+
expect(powerSort([5, 4])).toEqual([4, 5])
14+
15+
expect(powerSort([8, 4, 10, 15, 9])).toEqual([4, 8, 9, 10, 15])
16+
17+
expect(powerSort([1, 2, 3])).toEqual([1, 2, 3])
18+
19+
expect(powerSort([10, 5, 3, 8, 2, 6, 4, 7, 9, 1])).toEqual([
20+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
21+
])
22+
})
23+
24+
it('should sort arrays containing duplicate values', () => {
25+
expect(powerSort([4, 2, 4, 1, 2, 3, 1])).toEqual([1, 1, 2, 2, 3, 4, 4])
26+
})
27+
28+
it('should handle already sorted arrays', () => {
29+
expect(powerSort([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6])
30+
})
31+
32+
it('should handle reverse-sorted arrays', () => {
33+
expect(powerSort([9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([
34+
1, 2, 3, 4, 5, 6, 7, 8, 9
35+
])
36+
})
37+
38+
it('should sort arrays larger than MIN_RUN', () => {
39+
const array = Array.from({ length: 100 }, (_, index) => 100 - index)
40+
41+
expect(powerSort(array)).toEqual(
42+
Array.from({ length: 100 }, (_, index) => index + 1)
43+
)
44+
})
45+
46+
it('should return the same array after sorting', () => {
47+
const array = [5, 3, 1, 4, 2]
48+
49+
const result = powerSort(array)
50+
51+
expect(result).toBe(array)
52+
expect(array).toEqual([1, 2, 3, 4, 5])
53+
})
54+
55+
it('should merge multiple natural runs correctly', () => {
56+
const array = [
57+
...Array.from({ length: 32 }, (_, i) => i + 96),
58+
...Array.from({ length: 32 }, (_, i) => i + 64),
59+
...Array.from({ length: 32 }, (_, i) => i + 32),
60+
...Array.from({ length: 32 }, (_, i) => i)
61+
]
62+
63+
expect(powerSort(array)).toEqual(Array.from({ length: 128 }, (_, i) => i))
64+
})
65+
66+
it('should merge natural runs of different lengths', () => {
67+
const array = [
68+
...Array.from({ length: 32 }, (_, i) => i + 200),
69+
...Array.from({ length: 40 }, (_, i) => i + 150),
70+
...Array.from({ length: 35 }, (_, i) => i + 100),
71+
...Array.from({ length: 50 }, (_, i) => i)
72+
]
73+
74+
const expected = [
75+
...Array.from({ length: 50 }, (_, i) => i),
76+
...Array.from({ length: 35 }, (_, i) => i + 100),
77+
...Array.from({ length: 40 }, (_, i) => i + 150),
78+
...Array.from({ length: 32 }, (_, i) => i + 200)
79+
]
80+
81+
expect(powerSort(array)).toEqual(expected)
82+
})
83+
})

0 commit comments

Comments
 (0)