Skip to content

Add a Quicksort implementation. #15380

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

Closed
wants to merge 1 commit into from

Conversation

SimonSapin
Copy link
Contributor

Quicksort makes different tradeoffs than the existing merge sort: It does not allocate, but is not stable.

r? @pcwalton

Quicksort makes different tradeoffs than the existing merge sort:
It does not allocate, but is not stable.

Original implementation by @pcwalton:
servo/servo@3764c3c49f
@brson
Copy link
Contributor

brson commented Jul 3, 2014

We have previously made the decision not to include multiple sorting routines in favor of one stable sort that works well.

@alexcrichton
Copy link
Member

Agreed, exposing two methods for sorting on vectors by default can get confusing, especially when one sounds like it's faster than the other (quicksort vs sort).

That being said, it is useful to be able to sort in libcore without the standard library. Can you elaborate more about why you'd like to add this, given our initial decision to not have multiple sorting routines?

@SimonSapin
Copy link
Contributor Author

According to @pcwalton, memory allocation used to be a significant part of the time spent doing selector matching in Servo (which involves sorting CSS declarations by specificity). We switched to quicksort (which does not allocate) for speed.

I understand the concern of having "competing" methods. What if I removed all the methods in this PR, so that it only adds a function?

@alexcrichton
Copy link
Member

If the stdlib sort is inadequate, then we may wish to investigate replacing it instead of adding another. Until that time, if this is purely for performance in a subset of the work sort does, I would think that this could evolve out of libcore rather than inside of it.

@SimonSapin
Copy link
Contributor Author

Do you mean an a library other than libcore in this repository, or outside of this repository?

@alexcrichton
Copy link
Member

Outside of this repository

@steveklabnik
Copy link
Member

Especially as Cargo comes along... Quicksort is the kind of thing that I wouldn't mind being in the standard library, as it isn't likely to change a whole lot, but just a thought.

@bluss
Copy link
Member

bluss commented Jul 3, 2014

Should it be introsort (a quicksort variant) instead, to keep the same complexity even against maliciously constructed input? The C++ STL introsort is apparently very fast, so I guess the algorithm shouldn't cost any performance.

@alexcrichton
Copy link
Member

We discussed this on IRC and the conclusion for now is that not having two sorts is the paramount concern here. Servo can definitely have its own sort, and we can possibly consider adding a variant to the standard library that works alongside the existing sort method.

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 7, 2023
Fix unsized struct problems in mir eval
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.

5 participants