Add Shell sort implementation with tests#1319
Merged
Merged
Conversation
Implement Shell sort as a gapped insertion sort using the original Shell gap sequence (n/2, n/4, ..., 1), following the repo's sorting conventions: - ShellSort implements the shared InplaceSort interface, with a teaching doc comment (how/why, time/space complexity) and a runnable main method. - ShellSortTest mirrors the existing sort tests (edge cases + randomized). - Register SHELL_SORT in the shared SortingTest enum/EnumSet so it is covered by the cross-algorithm property tests. - Add Bazel java_binary / java_test targets and a README entry. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Sculptor <sculptor@imbue.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds a Shell sort implementation to the sorting package, following the repo's existing sorting conventions.
Shell sort is implemented as a gapped insertion sort using the original Shell gap sequence (
n/2, n/4, …, 1). The large-gap passes move out-of-place elements most of the way toward their final position in big jumps, so the final gap-1 pass (an ordinary insertion sort) has very little left to do.Changes
ShellSort.java— implements the sharedInplaceSortinterface, with a teaching-style doc comment explaining how and why the algorithm works, its time/space complexity, and a runnablemainmethod (matches the style ofInsertionSort/TimSort).ShellSortTest.java— JUnit 5 + Truth tests mirroring the existing sort tests: empty, single, already-sorted, reverse-sorted, duplicates, all-same, negatives, mixed sign, two-element, plus a randomized pass over sizes 0–499 compared againstArrays.sort.SortingTest.java— registersSHELL_SORTin the shared enum/EnumSetso it's also covered by the cross-algorithm property tests.BUILDfiles — add theShellSortjava_binarytarget and theShellSortTestjava_testtarget.README.md— add a Shell sort entry to the sorting algorithms list.Complexity
O(n²)worst case with the Shell sequence, ~O(n^1.5)average,O(n log n)best case (already sorted).O(1)— sorts in place.Testing
bazel test //src/test/java/com/williamfiset/algorithms/sorting:ShellSortTest //src/test/java/com/williamfiset/algorithms/sorting:SortingTest→ both PASSED.bazel run //src/main/java/com/williamfiset/algorithms/sorting:ShellSortprints[-13, 2, 3, 4, 4, 6, 8, 10]as documented.