Skip to content

Commit 9c03738

Browse files
committed
fix: logic simplication
1 parent 4da7154 commit 9c03738

1 file changed

Lines changed: 29 additions & 65 deletions

File tree

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.thealgorithms.sorts;
22
// author: Vraj Prajapati @Rosander0
33

4-
import java.util.Arrays;
5-
64
/**
75
* Library Sort (also known as Gapped Insertion Sort) is a sorting algorithm
86
* that works like insertion sort but leaves gaps between elements to make
@@ -19,6 +17,8 @@ private LibrarySort() {
1917
// Utility class
2018
}
2119

20+
private static final int GAP = 1;
21+
2222
/**
2323
* Sorts an array using the Library Sort algorithm.
2424
*
@@ -34,93 +34,57 @@ public static int[] sort(final int[] array) {
3434
}
3535

3636
int n = array.length;
37-
int gapSize = 1;
38-
int[] library = new int[2 * n];
39-
boolean[] occupied = new boolean[2 * n];
40-
Arrays.fill(library, Integer.MAX_VALUE);
37+
// Allocate spaced array with gaps between each element
38+
int spacedSize = n * (GAP + 1);
39+
Integer[] spaced = new Integer[spacedSize];
4140

4241
// Insert first element
43-
library[0] = array[0];
44-
occupied[0] = true;
45-
int occupiedCount = 1;
42+
spaced[0] = array[0];
43+
44+
int inserted = 1;
4645

4746
for (int i = 1; i < n; i++) {
48-
// Find insertion position using binary search
49-
int pos = binarySearch(library, occupied, array[i]);
47+
// Binary search for correct position among inserted elements
48+
int pos = binarySearch(spaced, inserted, array[i]);
5049

51-
// Shift elements to make room
52-
if (occupied[pos]) {
53-
// Find next free space
54-
int free = pos;
55-
while (free < 2 * n && occupied[free]) {
56-
free++;
57-
}
58-
if (free >= 2 * n) {
59-
// Rebalance if no space found
60-
rebalance(library, occupied, n);
61-
pos = binarySearch(library, occupied, array[i]);
62-
free = pos;
63-
while (free < 2 * n && occupied[free]) {
64-
free++;
65-
}
66-
}
67-
// Shift right to create gap
68-
for (int j = free; j > pos; j--) {
69-
library[j] = library[j - 1];
70-
occupied[j] = occupied[j - 1];
71-
}
50+
// Shift elements right by GAP+1 to make room
51+
for (int j = inserted; j > pos; j--) {
52+
spaced[j] = spaced[j - 1];
7253
}
73-
library[pos] = array[i];
74-
occupied[pos] = true;
75-
occupiedCount++;
76-
gapSize++;
54+
spaced[pos] = array[i];
55+
inserted++;
7756
}
7857

79-
// Collect sorted elements
58+
// Collect sorted elements back
8059
int idx = 0;
81-
for (int i = 0; i < 2 * n; i++) {
82-
if (occupied[i]) {
83-
array[idx++] = library[i];
60+
for (int i = 0; i < spacedSize; i++) {
61+
if (spaced[i] != null) {
62+
array[idx++] = spaced[i];
8463
}
8564
}
8665
return array;
8766
}
8867

8968
/**
90-
* Binary search to find insertion position in the library array.
69+
* Binary search to find insertion position among inserted elements.
70+
*
71+
* @param spaced the spaced array
72+
* @param inserted number of elements inserted so far
73+
* @param target the value to find position for
74+
* @return the correct insertion index
9175
*/
92-
private static int binarySearch(final int[] library, final boolean[] occupied, final int target) {
76+
private static int binarySearch(final Integer[] spaced,
77+
final int inserted, final int target) {
9378
int lo = 0;
94-
int hi = library.length - 1;
79+
int hi = inserted;
9580
while (lo < hi) {
9681
int mid = lo + (hi - lo) / 2;
97-
if (library[mid] <= target) {
82+
if (spaced[mid] != null && spaced[mid] <= target) {
9883
lo = mid + 1;
9984
} else {
10085
hi = mid;
10186
}
10287
}
10388
return lo;
10489
}
105-
106-
/**
107-
* Rebalances the library array by redistributing elements with gaps.
108-
*/
109-
private static void rebalance(final int[] library, final boolean[] occupied, final int n) {
110-
int[] temp = new int[n];
111-
int idx = 0;
112-
for (int i = 0; i < 2 * n; i++) {
113-
if (occupied[i]) {
114-
temp[idx++] = library[i];
115-
occupied[i] = false;
116-
library[i] = Integer.MAX_VALUE;
117-
}
118-
}
119-
// Redistribute with gaps
120-
for (int i = 0; i < idx; i++) {
121-
int pos = 2 * i;
122-
library[pos] = temp[i];
123-
occupied[pos] = true;
124-
}
125-
}
12690
}

0 commit comments

Comments
 (0)