Skip to content

Fix Perfect pivot selecting an index outside the current partition - #365

Open
B7M wants to merge 1 commit into
RodrigoDLPontes:masterfrom
B7M:fix/perfect-pivot-partition-bounds
Open

Fix Perfect pivot selecting an index outside the current partition#365
B7M wants to merge 1 commit into
RodrigoDLPontes:masterfrom
B7M:fix/perfect-pivot-partition-bounds

Conversation

@B7M

@B7M B7M commented Aug 27, 2026

Copy link
Copy Markdown

Fix Perfect pivot choosing an index outside the current partition

---------- PR DESCRIPTION BELOW THIS LINE ----------

Problem

In Perfect pivot mode, Quicksort and Quickselect used this code to find the pivot:

pivot = this.arrayData.indexOf(sorted[midIndex]);

The problem is that indexOf starts searching from the beginning of the whole array. The pivot should only be selected from the current partition, between left and right.

If the array has duplicate values, indexOf can find the same value at an earlier index outside the current partition. The algorithm then swaps the wrong element.

Because of this:

  • Quicksort can finish with an unsorted array.
  • Quickselect can return the wrong k-th element.

Fix

I added left as the starting position for indexOf:

-pivot = this.arrayData.indexOf(sorted[midIndex]);
+pivot = this.arrayData.indexOf(sorted[midIndex], left);

Now it only searches from the start of the current partition. The pivot value will be found there because sorted is made from the same partition.

I made this change in src/algo/Quicksort.js and src/algo/Quickselect.js. The other pivot modes were not changed.

Testing

I tested the change in the browser and checked the final arrays.

Quicksort

Input Before After Expected
1,2,1,1 2 1b 1c 1a 1a 1b 1c 2 1 1 1 2
3,2,2,2 3 2b 2c 2a 2a 2b 2c 3 2 2 2 3
1,3,1,1 3 1b 1c 1a 1a 1b 1c 3 1 1 1 3
2,1,1,1 2 1b 1c 1a 1a 1b 1c 2 1 1 1 2

I also tested 5,5,5,5, 7,3,7,3,7, 9,1,9,1,9,1, 2,2,1,3,3,1, 6,6,6,2,6,9,1, 1,1,2,2,3,3, and 8,2,8,2,8,2,8. All sorted correctly after the fix. 5,3,1,4,2 (no duplicates) was correct before and after, since the bug needs duplicates to trigger. This is also why the examples in the dropdown never show the problem — they all use distinct values.

Quickselect

I tested 1,2,1,1 with k=4. The correct answer is 2.

  • Before the fix, it returned 1
  • After the fix, it returned 2

I also tested k=1 on 1,2,1,1 and 3,2,2,2, k=4 on 3,2,2,2, k=6 on 9,1,9,1,9,1, and k=3 on 5,3,1,4,2. All returned the correct result after the fix.

Checks

  • npm run lint passed
  • Prettier check passed for both changed files
  • Only the Quicksort and Quickselect files were changed

@vercel

vercel Bot commented Aug 27, 2026

Copy link
Copy Markdown

@B7M is attempting to deploy a commit to the rodrigodlpontes' projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant