Repository navigation
Expand file tree
/
Copy pathLibrarySort.java
More file actions
207 lines (189 loc) · 7.55 KB
/
Copy pathLibrarySort.java
File metadata and controls
207 lines (189 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.thealgorithms.sorts;
import java.util.Arrays;
/**
* Library Sort (also known as Gapped Insertion Sort) maintains a sparse
* working array with gaps distributed between elements, so that most
* insertions land directly in an empty gap without shifting anything.
* Elements are inserted in rounds that double in size (1, 2, 4, 8, ...);
* after each round the array is rebalanced so gaps are spread out evenly
* again for the next round.
* Time Complexity: O(n log n) expected, O(n^2) worst case if gaps collapse
* Space Complexity: O(n)
*
* @see <a href="https://en.wikipedia.org/wiki/Library_sort">
* Wikipedia: Library Sort</a>
* @author Vraj Prajapati (@Rosander0)
*/
public final class LibrarySort {
private static final int GAP_FACTOR = 2;
private LibrarySort() {
// Utility class
}
/**
* Sorts an array using the Library Sort algorithm.
*
* @param array the array to sort (must not be null)
* @return the sorted array
* @throws IllegalArgumentException if {@code array} is {@code null}
*/
public static int[] sort(final int[] array) {
if (array == null) {
throw new IllegalArgumentException("Input array must not be null.");
}
if (array.length <= 1) {
return array;
}
final int n = array.length;
final int capacity = GAP_FACTOR * n;
final int[] data = new int[capacity];
final boolean[] occupied = new boolean[capacity];
final int mid = capacity / 2;
data[mid] = array[0];
occupied[mid] = true;
int filled = 1;
int nextToInsert = 1;
int round = 0;
while (nextToInsert < n) {
final int roundSize = Math.min(1 << round, n - nextToInsert);
for (int i = 0; i < roundSize; i++) {
insert(data, occupied, array[nextToInsert + i]);
filled++;
}
nextToInsert += roundSize;
round++;
if (nextToInsert < n) {
rebalance(data, occupied, filled);
}
}
int idx = 0;
for (int i = 0; i < capacity; i++) {
if (occupied[i]) {
array[idx++] = data[i];
}
}
return array;
}
/**
* Inserts {@code value} into the gapped array, placing it directly in an
* empty gap when possible, otherwise shifting toward the nearest gap.
*/
private static void insert(final int[] data, final boolean[] occupied, final int value) {
final int pos = findInsertionIndex(data, occupied, value);
if (pos >= data.length) {
insertAtEnd(data, occupied, value);
return;
}
if (!occupied[pos]) {
data[pos] = value;
occupied[pos] = true;
return;
}
int right = pos;
while (right < data.length && occupied[right]) {
right++;
}
int left = pos - 1;
while (left >= 0 && occupied[left]) {
left--;
}
final boolean canGoRight = right < data.length;
final boolean canGoLeft = left >= 0;
if (canGoRight && (!canGoLeft || (right - pos) <= (pos - left))) {
// Shift data[pos, right) one slot to the right, opening a gap at pos.
// occupied[pos] is untouched by the copy and was already true.
System.arraycopy(data, pos, data, pos + 1, right - pos);
occupied[right] = true;
data[pos] = value;
} else if (canGoLeft) {
// Shift data[left + 1, pos) one slot to the left, opening a gap at pos - 1.
// occupied[pos - 1] is untouched by the copy and was already true.
System.arraycopy(data, left + 1, data, left, pos - 1 - left);
occupied[left] = true;
data[pos - 1] = value;
} else {
// Unreachable in practice: canGoRight and canGoLeft can only both be false if
// every slot in this capacity-2n array is occupied, but at most n elements are
// ever present at once. Kept as a defensive guard against that invariant breaking.
throw new IllegalStateException("No gap available for insertion; rebalance too infrequent.");
}
}
/**
* Handles insertion of a new global maximum, which must land after every
* currently occupied slot. Since there is no room to its right, this
* shifts occupied slots left into the nearest gap instead.
*/
private static void insertAtEnd(final int[] data, final boolean[] occupied, final int value) {
final int last = data.length - 1;
// occupied[last] is unreachable as false here: insertAtEnd() is only called when
// findInsertionIndex() returns data.length, which requires data[last] to already be
// occupied. Kept as a defensive guard in case that invariant is ever broken.
if (!occupied[last]) {
data[last] = value;
occupied[last] = true;
return;
}
int left = last - 1;
while (left >= 0 && occupied[left]) {
left--;
}
// left < 0 is unreachable in practice: at most n elements ever occupy this
// capacity-2n array, so fewer than half the slots left of `last` can be filled,
// guaranteeing a gap exists before the scan reaches index -1.
if (left < 0) {
throw new IllegalStateException("No gap available for insertion; rebalance too infrequent.");
}
// Shift data[left + 1, last] one slot to the left, opening a gap at last.
// occupied[last] is untouched by the copy and was already true.
System.arraycopy(data, left + 1, data, left, last - left);
occupied[left] = true;
data[last] = value;
}
/**
* Finds the leftmost index at which {@code value} can be inserted so
* that occupied slots remain sorted. Empty slots are compared using the
* value of the nearest occupied slot at or after them, which is a
* monotonic function of index and therefore safe to binary search over.
*/
private static int findInsertionIndex(final int[] data, final boolean[] occupied, final int value) {
int lo = 0;
int hi = data.length;
while (lo < hi) {
final int mid = lo + (hi - lo) / 2;
final int probe = nearestOccupiedValueAtOrAfter(data, occupied, mid);
if (probe != Integer.MAX_VALUE && probe <= value) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo;
}
private static int nearestOccupiedValueAtOrAfter(final int[] data, final boolean[] occupied, final int index) {
for (int i = index; i < data.length; i++) {
if (occupied[i]) {
return data[i];
}
}
return Integer.MAX_VALUE;
}
/**
* Redistributes the {@code filled} occupied elements evenly across the
* full capacity of {@code data}, restoring uniform gaps between them.
*/
private static void rebalance(final int[] data, final boolean[] occupied, final int filled) {
final int capacity = data.length;
final int[] temp = new int[filled];
int idx = 0;
for (int i = 0; i < capacity; i++) {
if (occupied[i]) {
temp[idx++] = data[i];
}
}
Arrays.fill(occupied, false);
for (int k = 0; k < filled; k++) {
final int pos = (int) ((long) k * capacity / filled);
data[pos] = temp[k];
occupied[pos] = true;
}
}
}