Skip to content

Conversation

@qalisander
Copy link
Member

@qalisander qalisander commented Jan 20, 2026

Casual quick sort in-place implementation similar to openzeppelin-solidity qsort.

Compare to referenced (in issue) implementation it utilities stack data structure, since call stack (recursion) is not supported within move macro. Move macro cannot call itself.

Resolves #95

PR Checklist

  • Tests
  • Documentation
  • Changelog

Summary by CodeRabbit

  • New Features

    • Added in-place vector sorting functionality with new public macros for efficient sorting of unsigned integer vectors.
    • Support for multiple integer widths (u8, u16, u32, u128, u256).
  • Tests

    • Comprehensive test suite added covering edge cases, boundary conditions, and various data patterns.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 20, 2026

Walkthrough

Introduces two new public macros quick_sort and partition to math/core/sources/internal/macros.move for in-place sorting of unsigned integer vectors using iterative quicksort with explicit stack and Lomuto partitioning. Adds extensive test suite covering empty, single-element, sorted, reverse-sorted, duplicate, and edge-case vectors across multiple integer widths.

Changes

Cohort / File(s) Summary
Quicksort Implementation
math/core/sources/internal/macros.move
Added public macro quick_sort<$Int> for in-place iterative quicksort using an explicit stack; added public macro partition<$Int> for Lomuto partitioning that returns pivot index; stack-based approach avoids recursion depth limits
Test Suite
math/core/tests/macros_tests.move
Added 496 lines of comprehensive unit tests validating quick_sort across empty vectors, single elements, small fixed-size vectors, already-sorted and reverse-sorted inputs, duplicates, mixed value ranges, multiple integer types (u8, u16, u32, u128, u256), randomized patterns, boundary values, and consistency checks for correctness

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A quicksort dance, so neat and swift,
With stacks of indices, a clever gift!
Partition, pivot, left and right,
Your vectors sorted—pure delight!
One hundred tests to make it tight! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title mentions 'quick_sort_by implementations' but the raw summary shows only quick_sort was implemented, not quick_sort_by. The title is misleading about the actual scope of changes. Update the title to 'feat: add quick_sort implementation' to accurately reflect that only quick_sort (not quick_sort_by) was added to the codebase.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description covers the purpose, context, approach, issue resolution, and includes a completed checklist. Tests and documentation are marked complete, with only changelog pending.
Linked Issues check ✅ Passed The PR fulfills issue #95 by providing a quick_sort implementation adapted from the referenced pattern, using an explicit stack for Move macro compatibility.
Out of Scope Changes check ✅ Passed All changes are directly related to the quick_sort feature: implementation of quick_sort and partition macros, and comprehensive test coverage with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/quick-sort

Comment @coderabbitai help to get the list of available commands and usage tips.

@qalisander qalisander changed the title Feat/quick sort quick sort implementation Jan 20, 2026
@qalisander qalisander changed the title quick sort implementation 'quick_sort' implementation Jan 20, 2026
@codecov
Copy link

codecov bot commented Jan 20, 2026

Codecov Report

❌ Patch coverage is 30.00000% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.29%. Comparing base (939f59f) to head (83bfbee).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
math/core/sources/vector.move 30.00% 14 Missing ⚠️

❌ Your patch status has failed because the patch coverage (30.00%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #133       +/-   ##
===========================================
+ Coverage   74.82%   95.29%   +20.47%     
===========================================
  Files          13       19        +6     
  Lines         433     1468     +1035     
  Branches      110      385      +275     
===========================================
+ Hits          324     1399     +1075     
+ Misses        106       48       -58     
- Partials        3       21       +18     
Flag Coverage Δ
contracts/access 50.19% <30.00%> (-3.32%) ⬇️
math/core 94.21% <30.00%> (?)
math/fixed_point 60.84% <0.00%> (-4.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@math/core/sources/internal/macros.move`:
- Around line 988-1038: The documentation incorrectly claims stability and
left-first processing for the quick_sort<$Int> macro; the implementation using
Lomuto and an explicit LIFO stack is not stable and currently processes the
right partition first. Fix by (a) removing or changing the stability/left-first
wording in the macro doc comments, and (b) change the stack push order in
quick_sort so the larger partition is pushed first and the smaller partition
pushed second (so the smaller partition is popped/processed next) — update the
push_back sequence around the partition index returned by partition!(vec, start,
end) (references: quick_sort macro, partition! invocation, stack_start and
stack_end vectors and their push_back calls).

In `@math/core/tests/macros_tests.move`:
- Around line 1854-1860: Reformat the Move test file to satisfy Prettier: run
the provided formatter (npx prettier-move -w **/*.move) or apply equivalent
formatting to the block around the sorting assertion (the while loop using
variables vec, len, i and the assert! call) so spacing/line breaks match project
style, then re-commit the updated file.

@bidzyyys bidzyyys changed the title 'quick_sort' implementation feat: add quick_sort implementation Jan 21, 2026
Copy link
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

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

Looking good, we also need to make this function accesible by exposing it in the corresponding integer modules.

@qalisander
Copy link
Member Author

Added quick_sort_by and quick_sort (based on quick_sort_by), made macro public and moved to vector module.

Copy link
Collaborator

@bidzyyys bidzyyys left a comment

Choose a reason for hiding this comment

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

Great job @qalisander!

@qalisander qalisander changed the title feat: add quick_sort implementation feat: add quick_sort and quick_sort_by implementations Jan 22, 2026
Copy link
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

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

LGTM!

@bidzyyys bidzyyys merged commit d463248 into main Jan 27, 2026
13 of 14 checks passed
@bidzyyys bidzyyys deleted the feat/quick-sort branch January 27, 2026 12:07
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.

[Feature]: quick_sort

4 participants