-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Add LibrarySort Implementation #7481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rosander0
wants to merge
1
commit into
TheAlgorithms:master
Choose a base branch
from
Rosander0:add-library-sort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| /** | ||
| * Library Sort (also known as Gapped Insertion Sort) is traditionally implemented | ||
| * using periodic gaps between elements for faster insertion. This implementation | ||
| * uses binary search to find the insertion position combined with array shifting, | ||
| * which is a simplified variant without gap-based optimization. | ||
| * Time Complexity: O(n^2) worst case due to element shifting | ||
| * 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 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; | ||
| } | ||
|
|
||
| int n = array.length; | ||
| Integer[] spaced = new Integer[2 * n]; | ||
|
|
||
| spaced[0] = array[0]; | ||
| int inserted = 1; | ||
|
|
||
| for (int i = 1; i < n; i++) { | ||
| int pos = binarySearch(spaced, inserted, array[i]); | ||
| for (int j = inserted; j > pos; j--) { | ||
| spaced[j] = spaced[j - 1]; | ||
| } | ||
| spaced[pos] = array[i]; | ||
| inserted++; | ||
| } | ||
|
|
||
| int idx = 0; | ||
| for (int i = 0; i < 2 * n; i++) { | ||
| if (spaced[i] != null) { | ||
| array[idx++] = spaced[i]; | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
|
|
||
| /** | ||
| * Binary search to find insertion position among inserted elements. | ||
| * | ||
| * @param spaced the spaced array | ||
| * @param inserted number of elements inserted so far | ||
| * @param target the value to find position for | ||
| * @return the correct insertion index | ||
| */ | ||
| private static int binarySearch(final Integer[] spaced, final int inserted, final int target) { | ||
| int lo = 0; | ||
| int hi = inserted; | ||
| while (lo < hi) { | ||
| int mid = lo + (hi - lo) / 2; | ||
| if (spaced[mid] <= target) { | ||
| lo = mid + 1; | ||
| } else { | ||
| hi = mid; | ||
| } | ||
| } | ||
| return lo; | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/test/java/com/thealgorithms/sorts/LibrarySortTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.thealgorithms.sorts; | ||
| // author: Vraj Prajapati @Rosander0 | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class LibrarySortTest { | ||
|
|
||
| @Test | ||
| public void testBasicSort() { | ||
| assertArrayEquals(new int[] {1, 2, 3, 4, 5}, LibrarySort.sort(new int[] {5, 3, 1, 4, 2})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAlreadySorted() { | ||
| assertArrayEquals(new int[] {1, 2, 3, 4, 5}, LibrarySort.sort(new int[] {1, 2, 3, 4, 5})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReverseSorted() { | ||
| assertArrayEquals(new int[] {1, 2, 3, 4, 5}, LibrarySort.sort(new int[] {5, 4, 3, 2, 1})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDuplicates() { | ||
| assertArrayEquals(new int[] {1, 2, 2, 3, 3}, LibrarySort.sort(new int[] {3, 2, 1, 3, 2})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleElement() { | ||
| assertArrayEquals(new int[] {1}, LibrarySort.sort(new int[] {1})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyArray() { | ||
| assertArrayEquals(new int[] {}, LibrarySort.sort(new int[] {})); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullArray() { | ||
| assertThrows(IllegalArgumentException.class, () -> LibrarySort.sort(null)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.