Skip to content

Commit fe6066b

Browse files
authored
Add SmoothSort (Dijkstra’s adaptive in-place heapsort variant) (#7200)
* feat: implement Smooth Sort algorithm with detailed JavaDoc and test class * style: format LEONARDO array for improved readability with clang-format --------- Co-authored-by: Ahmed Allam <[email protected]>
1 parent ca4bebc commit fe6066b

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
4+
* Smooth Sort is an in-place, comparison-based sorting algorithm proposed by Edsger W. Dijkstra (1981).
5+
*
6+
* <p>It can be viewed as a variant of heapsort that maintains a forest of heap-ordered Leonardo trees
7+
* (trees whose sizes are Leonardo numbers). The algorithm is adaptive: when the input is already
8+
* sorted or nearly sorted, the heap invariants are often satisfied and the expensive rebalancing
9+
* operations do little work, yielding near-linear behavior.
10+
*
11+
* <p>Time Complexity:
12+
* <ul>
13+
* <li>Best case: O(n) for already sorted input</li>
14+
* <li>Average case: O(n log n)</li>
15+
* <li>Worst case: O(n log n)</li>
16+
* </ul>
17+
*
18+
* <p>Space Complexity: O(1) auxiliary space (in-place).
19+
*
20+
* @see <a href="https://en.wikipedia.org/wiki/Smoothsort">Smoothsort</a>
21+
* @see <a href="https://en.wikipedia.org/wiki/Leonardo_number">Leonardo numbers</a>
22+
* @see SortAlgorithm
23+
*/
24+
public class SmoothSort implements SortAlgorithm {
25+
26+
/**
27+
* Leonardo numbers (L(0) = L(1) = 1, L(k+2) = L(k+1) + L(k) + 1) up to the largest value that
28+
* fits into a signed 32-bit integer.
29+
*/
30+
private static final int[] LEONARDO = {1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049, 242785, 392835, 635621, 1028457, 1664079, 2692537, 4356617, 7049155, 11405773, 18454929, 29860703, 48315633, 78176337,
31+
126491971, 204668309, 331160281, 535828591, 866988873, 1402817465};
32+
33+
/**
34+
* Sorts the given array in ascending order using Smooth Sort.
35+
*
36+
* @param array the array to sort
37+
* @param <T> the element type
38+
* @return the sorted array
39+
*/
40+
@Override
41+
public <T extends Comparable<T>> T[] sort(final T[] array) {
42+
if (array.length < 2) {
43+
return array;
44+
}
45+
46+
final int last = array.length - 1;
47+
48+
// The forest shape is encoded as (p, pshift): p is a bit-vector of present tree orders,
49+
// shifted right by pshift. pshift is the order of the rightmost (current) Leonardo tree.
50+
long p = 1L;
51+
int pshift = 1;
52+
53+
int head = 0;
54+
while (head < last) {
55+
if ((p & 3L) == 3L) {
56+
sift(array, pshift, head);
57+
p >>>= 2;
58+
pshift += 2;
59+
} else {
60+
// Add a new singleton tree; if it will not be merged anymore, we must fully trinkle.
61+
if (LEONARDO[pshift - 1] >= last - head) {
62+
trinkle(array, p, pshift, head, false);
63+
} else {
64+
// This tree will be merged later, so it is enough to restore its internal heap property.
65+
sift(array, pshift, head);
66+
}
67+
68+
if (pshift == 1) {
69+
// If L(1) is used, the new singleton is L(0).
70+
p <<= 1;
71+
pshift = 0;
72+
} else {
73+
// Otherwise, shift to order 1 and append a singleton of order 1.
74+
p <<= (pshift - 1);
75+
pshift = 1;
76+
}
77+
}
78+
79+
p |= 1L;
80+
head++;
81+
}
82+
83+
trinkle(array, p, pshift, head, false);
84+
85+
// Repeatedly remove the maximum (always at head) by shrinking the heap region.
86+
while (pshift != 1 || p != 1L) {
87+
if (pshift <= 1) {
88+
// Rightmost tree is a singleton (order 0 or 1). Move to the previous tree root.
89+
final long mask = p & ~1L;
90+
final int shift = Long.numberOfTrailingZeros(mask);
91+
p >>>= shift;
92+
pshift += shift;
93+
} else {
94+
// Split a tree of order (pshift) into two children trees of orders (pshift-1) and (pshift-2).
95+
p <<= 2;
96+
p ^= 7L;
97+
pshift -= 2;
98+
99+
trinkle(array, p >>> 1, pshift + 1, head - LEONARDO[pshift] - 1, true);
100+
trinkle(array, p, pshift, head - 1, true);
101+
}
102+
103+
head--;
104+
}
105+
106+
return array;
107+
}
108+
109+
private static <T extends Comparable<T>> void sift(final T[] array, int order, int root) {
110+
final T value = array[root];
111+
112+
while (order > 1) {
113+
final int right = root - 1;
114+
final int left = root - 1 - LEONARDO[order - 2];
115+
116+
if (!SortUtils.less(value, array[left]) && !SortUtils.less(value, array[right])) {
117+
break;
118+
}
119+
120+
if (!SortUtils.less(array[left], array[right])) {
121+
array[root] = array[left];
122+
root = left;
123+
order -= 1;
124+
} else {
125+
array[root] = array[right];
126+
root = right;
127+
order -= 2;
128+
}
129+
}
130+
131+
array[root] = value;
132+
}
133+
134+
private static <T extends Comparable<T>> void trinkle(final T[] array, long p, int order, int root, boolean trusty) {
135+
final T value = array[root];
136+
137+
while (p != 1L) {
138+
final int stepson = root - LEONARDO[order];
139+
140+
if (!SortUtils.less(value, array[stepson])) {
141+
break;
142+
}
143+
144+
if (!trusty && order > 1) {
145+
final int right = root - 1;
146+
final int left = root - 1 - LEONARDO[order - 2];
147+
148+
if (!SortUtils.less(array[right], array[stepson]) || !SortUtils.less(array[left], array[stepson])) {
149+
break;
150+
}
151+
}
152+
153+
array[root] = array[stepson];
154+
root = stepson;
155+
156+
final long mask = p & ~1L;
157+
final int shift = Long.numberOfTrailingZeros(mask);
158+
p >>>= shift;
159+
order += shift;
160+
trusty = false;
161+
}
162+
163+
if (!trusty) {
164+
array[root] = value;
165+
sift(array, order, root);
166+
}
167+
}
168+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class SmoothSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new SmoothSort();
7+
}
8+
}

0 commit comments

Comments
 (0)