Skip to content

Sorter interface, static BinarySearch.indexOf, jqwik property tests - #8

Merged
diegourban merged 4 commits into
masterfrom
sorter
Nov 6, 2026
Merged

diegourban merged 4 commits into
masterfrom
sorter

Conversation

@diegourban

@diegourban diegourban commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Second of the three library-redesign PRs (sorting and search).

Sorting

  • New Sorter interface: void sort(int[] items). InsertionSort, SelectionSort, MergeSort and QuickSort implement it; their old (items, start, end) / (items, totalElements) methods are now private helpers.
  • The four per-class tests are replaced by SorterTest (examples, including duplicates, negatives, MIN_VALUE/MAX_VALUE and empty/single input) and SorterPropertyTest (jqwik: every sorter agrees with Arrays.sort on random arrays). Both run against all implementations.
  • QuickSort fix: partition always 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

  • New static BinarySearch.indexOf(int[] sorted, int key) returns an index or −1. It wraps the existing recursive find, now a private helper and otherwise unchanged, calling it with an inclusive upper bound of length - 1.
  • The old public find(items, from, to, key) read past the end of the array when called with to = length (as its own tests did) and a key above the maximum.
  • BinarySearchPropertyTest: agrees with Arrays.binarySearch on whether a key is present, and finds every element that is.

Tooling

  • jqwik 1.10.2 added as a test dependency, with default settings (1,000 tries; a failing sample is replayed first). .jqwik-database is git-ignored.

Verification

  • ./mvnw verify: 564 tests, coverage floors met.
  • Checked that the properties catch real bugs: breaking MergeSort's merge of the left half and BinarySearch's narrowing both made them fail, with minimal failing inputs (e.g. [-1, 0]).

🤖 Generated with Claude Code

diegourban and others added 3 commits November 6, 2026 15:32
- 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>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 10 complexity · 0 duplication

Metric Results
Complexity 10
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

⚠️ Resolve issues before merging

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Compute the midpoint without adding the two indices directly:

Suggested change
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-commenter

codecov-commenter commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.41%. Comparing base (8bf3a54) to head (73a7b44).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>
@diegourban
diegourban merged commit 7553bca into master Nov 6, 2026
2 checks passed
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.

2 participants