Skip to content

Commit 06718e9

Browse files
committed
Add SmoothSort algorithm
- Implements Smooth Sort using Leonardo heap structure - Adaptive sorting algorithm with O(n log n) worst case - Includes comprehensive test suite extending SortingAlgorithmTest
1 parent 9484c7e commit 06718e9

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
4+
* Smooth Sort Algorithm Implementation
5+
*
6+
* @see <a href="https://en.wikipedia.org/wiki/Smoothsort">Smooth Sort Algorithm</a>
7+
*/
8+
public class SmoothSort implements SortAlgorithm {
9+
10+
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};
11+
12+
@Override
13+
public <T extends Comparable<T>> T[] sort(T[] array) {
14+
if (array == null || array.length <= 1) {
15+
return array;
16+
}
17+
smoothSort(array, array.length);
18+
return array;
19+
}
20+
21+
private <T extends Comparable<T>> void smoothSort(T[] array, int n) {
22+
int q = 1;
23+
int r = 0;
24+
int p = 1;
25+
int b = 1;
26+
int c = 1;
27+
28+
while (q < n) {
29+
if ((p & 3) == 1) {
30+
sift(array, r, q);
31+
q++;
32+
r++;
33+
} else {
34+
if (LEONARDO[p - 1] < n - q) {
35+
sift(array, r, q);
36+
q += LEONARDO[p - 1];
37+
r++;
38+
p--;
39+
} else {
40+
sift(array, r, q);
41+
r = r + p - 1;
42+
p = c;
43+
q++;
44+
}
45+
}
46+
p++;
47+
}
48+
49+
for (int i = r; i > 0; i--) {
50+
if (p > 1) {
51+
p--;
52+
q = q - LEONARDO[p];
53+
trinkle(array, p, q, r);
54+
p--;
55+
q = q + LEONARDO[p];
56+
trinkle(array, p, q, r - 1);
57+
r--;
58+
}
59+
}
60+
}
61+
62+
private <T extends Comparable<T>> void sift(T[] array, int pshift, int head) {
63+
T val = array[head];
64+
while (pshift > 1) {
65+
int rt = head - 1;
66+
int lf = head - 1 - LEONARDO[pshift - 2];
67+
if (SortUtils.less(val, array[lf]) || SortUtils.less(val, array[rt])) {
68+
if (SortUtils.less(array[lf], array[rt])) {
69+
array[head] = array[rt];
70+
head = rt;
71+
pshift -= 1;
72+
} else {
73+
array[head] = array[lf];
74+
head = lf;
75+
pshift -= 2;
76+
}
77+
} else {
78+
break;
79+
}
80+
}
81+
array[head] = val;
82+
}
83+
84+
private <T extends Comparable<T>> void trinkle(T[] array, int p, int head, int tail) {
85+
T val = array[head];
86+
while (p > 0) {
87+
int stepson = head - LEONARDO[p];
88+
if (SortUtils.less(val, array[stepson])) {
89+
array[head] = array[stepson];
90+
head = stepson;
91+
p--;
92+
} else {
93+
break;
94+
}
95+
}
96+
array[head] = val;
97+
sift(array, p, head);
98+
}
99+
}
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)