Repository navigation
Sorter interface, static BinarySearch.indexOf, jqwik property tests - #8
Conversation
- Sorter: void sort(int[] items); Insertion, Selection, Merge and Quick sort implement it and keep their range helpers private - One example test (SorterTest) and one jqwik property (every sorter agrees with Arrays.sort) run against all four implementations - Fix QuickSort partition scanning from index 0 instead of the range start: results were correct but every sort was O(n^2) (200k ints: 4354 ms -> 9 ms) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BinarySearch.indexOf(int[] sorted, int key) returns an index or -1. Iterative, overflow-safe midpoint. The old find(items, from, to, key) read past the end of the array for keys above the maximum when called with to = length, as its tests did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 10 |
| Duplication | 0 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
The binary-search midpoint calculation should be reviewed for overflow-safe behavior. All required test scenarios are reported as present, and no additional quality or coverage concerns were identified.
Test suggestions
- Run common example-based sorting tests against insertion, selection, merge, and quick sort.
- Sort empty, singleton, already sorted, reverse-sorted, duplicate, negative, and MIN_VALUE/MAX_VALUE inputs.
- Validate every sorter against Arrays.sort over generated integer arrays.
- Find present values on both sides and at the boundaries of a sorted array.
- Return -1 for missing keys, keys outside the range, and empty arrays.
- Validate binary search presence behavior over generated sorted arrays, including duplicates.
Make this review better
TIP Add a ticket key to the branch or title so the reviewer can check the PR against it. Or tell us which ticket tracker you use here.
TIP Add Codacy review.md instructions or an AGENTS.md so the reviewer knows your conventions.
TIP How was this review? Give us feedback
| var low = 0; | ||
| var high = sorted.length - 1; | ||
| while (low <= high) { | ||
| var middle = (low + high) >>> 1; // unsigned shift: no overflow when low + high > Integer.MAX_VALUE |
There was a problem hiding this comment.
🟡 MEDIUM RISK
Compute the midpoint without adding the two indices directly:
| var middle = (low + high) >>> 1; // unsigned shift: no overflow when low + high > Integer.MAX_VALUE | |
| var middle = low + ((high - low) >>> 1); |
TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #8 +/- ##
============================================
- Coverage 92.46% 92.41% -0.06%
- Complexity 256 260 +4
============================================
Files 23 23
Lines 584 593 +9
Branches 94 94
============================================
+ Hits 540 548 +8
- Misses 25 26 +1
Partials 19 19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Keeps the agreed static indexOf API but goes back to the existing recursive find (now a private helper) instead of an iterative rewrite. indexOf calls it with an inclusive upper bound of length - 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Second of the three library-redesign PRs (sorting and search).
Sorting
Sorterinterface:void sort(int[] items).InsertionSort,SelectionSort,MergeSortandQuickSortimplement it; their old(items, start, end)/(items, totalElements)methods are now private helpers.SorterTest(examples, including duplicates, negatives,MIN_VALUE/MAX_VALUEand empty/single input) andSorterPropertyTest(jqwik: every sorter agrees withArrays.sorton random arrays). Both run against all implementations.partitionalways scanned from index 0 instead of the start of its range. Results were still correct, but every sort was O(n²). 200,000 random ints: 4354 ms → 9 ms; 1,000,000: 52 ms.Search
BinarySearch.indexOf(int[] sorted, int key)returns an index or −1. It wraps the existing recursivefind, now a private helper and otherwise unchanged, calling it with an inclusive upper bound oflength - 1.find(items, from, to, key)read past the end of the array when called withto = length(as its own tests did) and a key above the maximum.BinarySearchPropertyTest: agrees withArrays.binarySearchon whether a key is present, and finds every element that is.Tooling
.jqwik-databaseis git-ignored.Verification
./mvnw verify: 564 tests, coverage floors met.MergeSort's merge of the left half andBinarySearch's narrowing both made them fail, with minimal failing inputs (e.g.[-1, 0]).🤖 Generated with Claude Code