Skip to content

Add Shell sort implementation with tests#1319

Merged
williamfiset merged 1 commit into
masterfrom
william/zippy-dogfish
Jun 23, 2026
Merged

Add Shell sort implementation with tests#1319
williamfiset merged 1 commit into
masterfrom
william/zippy-dogfish

Conversation

@williamfiset

Copy link
Copy Markdown
Owner

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 shared InplaceSort interface, with a teaching-style doc comment explaining how and why the algorithm works, its time/space complexity, and a runnable main method (matches the style of InsertionSort/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 against Arrays.sort.
  • SortingTest.java — registers SHELL_SORT in the shared enum/EnumSet so it's also covered by the cross-algorithm property tests.
  • Bazel BUILD files — add the ShellSort java_binary target and the ShellSortTest java_test target.
  • README.md — add a Shell sort entry to the sorting algorithms list.

Complexity

  • Time: O(n²) worst case with the Shell sequence, ~O(n^1.5) average, O(n log n) best case (already sorted).
  • Space: 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:ShellSort prints [-13, 2, 3, 4, 4, 6, 8, 10] as documented.

Note: the new file uses @author Sculptor in its header, consistent with the recently added TimSort using a non-personal author tag. Happy to change it if a different attribution is preferred.

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>
@williamfiset williamfiset merged commit 08c6d5f into master Jun 23, 2026
2 checks passed
@williamfiset williamfiset deleted the william/zippy-dogfish branch June 23, 2026 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant