Skip to content

Commit 1427328

Browse files
committed
feat: add LibrarySort implementation
1 parent b3fcb12 commit 1427328

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.thealgorithms.sorts;
2+
// author: Vraj Prajapati @Rosander0
3+
4+
/**
5+
* Library Sort (also known as Gapped Insertion Sort) is a sorting algorithm
6+
* that works like insertion sort but leaves gaps between elements to make
7+
* future insertions faster.
8+
* Time Complexity: O(n log n) average case
9+
* Space Complexity: O(n)
10+
*
11+
* @see <a href="https://en.wikipedia.org/wiki/Library_sort">
12+
* Wikipedia: Library Sort</a>
13+
*/
14+
public final class LibrarySort {
15+
16+
private LibrarySort() {
17+
// Utility class
18+
}
19+
20+
/**
21+
* Sorts an array using the Library Sort algorithm.
22+
*
23+
* @param array the array to sort (must not be null)
24+
* @return the sorted array
25+
*/
26+
public static int[] sort(final int[] array) {
27+
if (array == null) {
28+
throw new IllegalArgumentException("Input array must not be null!");
29+
}
30+
if (array.length <= 1) {
31+
return array;
32+
}
33+
34+
int n = array.length;
35+
Integer[] spaced = new Integer[2 * n];
36+
37+
spaced[0] = array[0];
38+
int inserted = 1;
39+
40+
for (int i = 1; i < n; i++) {
41+
int pos = binarySearch(spaced, inserted, array[i]);
42+
for (int j = inserted; j > pos; j--) {
43+
spaced[j] = spaced[j - 1];
44+
}
45+
spaced[pos] = array[i];
46+
inserted++;
47+
}
48+
49+
int idx = 0;
50+
for (int i = 0; i < 2 * n; i++) {
51+
if (spaced[i] != null) {
52+
array[idx++] = spaced[i];
53+
}
54+
}
55+
return array;
56+
}
57+
58+
/**
59+
* Binary search to find insertion position among inserted elements.
60+
*
61+
* @param spaced the spaced array
62+
* @param inserted number of elements inserted so far
63+
* @param target the value to find position for
64+
* @return the correct insertion index
65+
*/
66+
private static int binarySearch(final Integer[] spaced, final int inserted, final int target) {
67+
int lo = 0;
68+
int hi = inserted;
69+
while (lo < hi) {
70+
int mid = lo + (hi - lo) / 2;
71+
if (spaced[mid] <= target) {
72+
lo = mid + 1;
73+
} else {
74+
hi = mid;
75+
}
76+
}
77+
return lo;
78+
}
79+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.sorts;
2+
// author: Vraj Prajapati @Rosander0
3+
4+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class LibrarySortTest {
10+
11+
@Test
12+
public void testBasicSort() {
13+
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, LibrarySort.sort(new int[] {5, 3, 1, 4, 2}));
14+
}
15+
16+
@Test
17+
public void testAlreadySorted() {
18+
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, LibrarySort.sort(new int[] {1, 2, 3, 4, 5}));
19+
}
20+
21+
@Test
22+
public void testReverseSorted() {
23+
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, LibrarySort.sort(new int[] {5, 4, 3, 2, 1}));
24+
}
25+
26+
@Test
27+
public void testDuplicates() {
28+
assertArrayEquals(new int[] {1, 2, 2, 3, 3}, LibrarySort.sort(new int[] {3, 2, 1, 3, 2}));
29+
}
30+
31+
@Test
32+
public void testSingleElement() {
33+
assertArrayEquals(new int[] {1}, LibrarySort.sort(new int[] {1}));
34+
}
35+
36+
@Test
37+
public void testEmptyArray() {
38+
assertArrayEquals(new int[] {}, LibrarySort.sort(new int[] {}));
39+
}
40+
41+
@Test
42+
public void testNullArray() {
43+
assertThrows(IllegalArgumentException.class, () -> LibrarySort.sort(null));
44+
}
45+
}

0 commit comments

Comments
 (0)