Skip to content

Fix excessive comparisons in minmax algorithms - #6264

Open
Mirion (mirion-dev) wants to merge 10 commits into
microsoft:mainfrom
mirion-dev:fix-minmax
Open

Mirion (mirion-dev) wants to merge 10 commits into
microsoft:mainfrom
mirion-dev:fix-minmax

Conversation

@mirion-dev

@mirion-dev Mirion (mirion-dev) commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

According to [alg.min.max#32], the algorithms should perform at most max(floor(3/2 (N−1)),0) comparisons, but the current implementations of std::{ranges::}minmax_element violate this requirement when N is even. This can be reproduced with the added test cases in this PR.

Although std::ranges::minmax does not violate it because its upper bound is (3/2) N [alg.min.max#23], I have optimized it as well.

@mirion-dev
Mirion (mirion-dev) requested a review from a team as a code owner April 28, 2026 11:40
@mirion-dev

This comment was marked as resolved.

@StephanTLavavej

This comment was marked as resolved.

Comment thread tests/std/tests/Dev11_0532622_minmax_element/test.cpp Outdated
Comment thread tests/std/tests/Dev11_0532622_minmax_element/test.cpp Outdated
Comment thread tests/std/tests/Dev11_0532622_minmax_element/test.cpp Outdated
Comment thread tests/std/tests/Dev11_0532622_minmax_element/test.cpp Outdated
Comment thread tests/std/tests/P0896R4_ranges_alg_minmax/test.cpp Outdated
Comment thread tests/std/tests/Dev11_0532622_minmax_element/test.cpp Outdated

This comment was marked as resolved.

Copilot AI review requested due to automatic review settings April 29, 2026 02:44

This comment was marked as resolved.

Comment thread tests/std/tests/Dev11_0532622_minmax_element/test.cpp Outdated
@mirion-dev

This comment was marked as resolved.

@StephanTLavavej

This comment was marked as resolved.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) added the bug Something isn't working label Apr 29, 2026
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Work In Progress to Initial Review in STL Code Reviews Apr 29, 2026
Copilot AI review requested due to automatic review settings September 25, 2026 15:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The revised pairing logic preserves extrema and tie behavior while satisfying the specified comparison bounds.

Review effort: Balanced
Findings: None

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

4 participants