Fix Perfect pivot selecting an index outside the current partition - #365
Open
B7M wants to merge 1 commit into
Open
Fix Perfect pivot selecting an index outside the current partition#365B7M wants to merge 1 commit into
B7M wants to merge 1 commit into
Conversation
|
@B7M is attempting to deploy a commit to the rodrigodlpontes' projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
The problem is that
indexOfstarts searching from the beginning of the whole array. The pivot should only be selected from the current partition, betweenleftandright.If the array has duplicate values,
indexOfcan find the same value at an earlier index outside the current partition. The algorithm then swaps the wrong element.Because of this:
Fix
I added
leftas the starting position forindexOf:Now it only searches from the start of the current partition. The pivot value will be found there because
sortedis made from the same partition.I made this change in
src/algo/Quicksort.jsandsrc/algo/Quickselect.js. The other pivot modes were not changed.Testing
I tested the change in the browser and checked the final arrays.
Quicksort
1,2,1,12 1b 1c 1a❌1a 1b 1c 2✅1 1 1 23,2,2,23 2b 2c 2a❌2a 2b 2c 3✅2 2 2 31,3,1,13 1b 1c 1a❌1a 1b 1c 3✅1 1 1 32,1,1,12 1b 1c 1a❌1a 1b 1c 2✅1 1 1 2I 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, and8,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,1withk=4. The correct answer is2.1❌2✅I also tested
k=1on1,2,1,1and3,2,2,2,k=4on3,2,2,2,k=6on9,1,9,1,9,1, andk=3on5,3,1,4,2. All returned the correct result after the fix.Checks
npm run lintpassed