-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sort
in memory to Arrays library
#4846
Conversation
🦋 Changeset detectedLatest commit: 4336e2e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: Ernesto García <ernestognw@gmail.com>
Co-authored-by: Ernesto García <ernestognw@gmail.com>
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
sort
in memory to Arrays library
Before approving I want to clarify why we're picking the first element as the pivot. I'm pending to dig more into this comment but I'd like to know if you got to a conclusion based on a previous discussion. |
There are a few elements of answer, but the bottom line is: its simpler that way, and there is no reason to think its a bad choice. Lets get into the issue: A each step, the subroutine sort a subarray by choosing a pivot. Putting all the values that are smaller on one side, all the values that are bigger on the other, and run recursivelly on both "sides". The pivot is placed correctly for sure. The other values are "grouped" to be sorted. Mathematically, the best pivot is on that divides the array in two sub-arrays of equal size. Said otherwise, the best pivot is the median. We have tree options:
The first option requires reading the values in a loop in order to make some decision. It means reading the array, which comes at a cost. I'm not conviced that choosing a "good" pivot would save move gas (due to the nice "split" it would produce) than the cost involved in choosing the good pivot If the array is shuffled, there is no reason to think that point 2 or 3 would be better. You have the same probability of getting a good or bad pivot. So it comes down to practicality. Since you don't want to have to pivot "in the way", a common approach is to set it asside, sort the rest of the array (into the 2 sections discussed earlier), and then put the pivot in the right place. The easiest way to set the pivot asside, is to put it at the beginning (or at the end), and then inverse it with the last element smaller than the pivot (or the first element bigger than the pivot). If you take your pivot randomly, putting it asside costs you one swap operation. If you choose the first (or the last) its already placed somewhere nice. So the bottom line is:
|
Great, I agree with this conclusions. I know there are other strategies to heuristically determine which element to use as the pivot. I think it's worth keeping in mind that there may be use cases where specifying the pivot as part of the params of Right now it's fine since the function is |
Replaces #3520 that is old. This comment remains relevant.
Fixes #3490
Fixes LIB-1189
IMO the approach should be to provide a simple/straight forward implementation that works. We can refine it/replace it down the line if we find a more efficient approach. Backward compatibility should not cause issues.
PR Checklist
npx changeset add
)