Skip to content

Conversation

Copy link

Copilot AI commented Jan 1, 2026

The partition function's inner loops lacked boundary checks, allowing indices to traverse beyond the [left, right] subarray being partitioned. This caused incorrect comparisons with elements outside the partition scope, particularly when the pivot was a minimum or maximum value.

Changes

  • Added i <= right check to prevent forward traversal beyond partition boundary
  • Added j >= left check to prevent backward traversal beyond partition boundary
  • Created test suite covering edge cases: pivot as min/max, duplicates, and sorted arrays

Example

// Before: i and j could access elements outside [left, right]
while (items[i] < pivot) {
    i++;
}

// After: boundary checks ensure indices stay within partition range
while (i <= right && items[i] < pivot) {
    i++;
}

The conditions use short-circuit evaluation, so array access only occurs when indices are in bounds.

Original prompt

quickSort breaks when the pivot is a minimum or maximum of the array. Fix it.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix quickSort breaking when pivot is minimum or maximum Add boundary checks to quicksort partition to prevent out-of-range access Jan 1, 2026
Copilot AI requested a review from nzakas January 1, 2026 20:23
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