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
[Feature Request] Adding a builtin sort
/sorted
function
#2390
Comments
I'm interested to support this feature. |
I think we need to implement comparison for CollectionElement
|
That'd be a good start :) |
This would be great! Right now we only have a basic sorting algorithm in the algorithm package that only works on AnyRegType if I recall correctly. Perhaps it makes sense to open source that as a reference and then we can make it work on AnyType, improve the performance of it, and use the ordering trait in #2462. Do you or @mzaks already have a generic sorting algorithm on AnyType/some refined trait such as the ordering one? If so, would very much welcome a PR into the library and we can build out this area. |
I don't have :( but willing to help on this! Perhaps building on what you have mentioned |
I updated mojo-sort so insertion sort and quick sort are working on nightly, I am currently using following signature: BTW if we will have a sorting package then I can also contribute Radix sort for numerical values. For large lists it is up to 30x faster than current std lib sort algorithm. Also in this case, a mix of insertion sort and Radix sort gives best performance, however important to know that Radix sort is not an in-place algorithm, so I would suggest to have it in std under an explicit name. |
And another point, there is a class of sorting algorithms specific for strings. https://github.com/rantala/string-sorting is a treasure trove when it comes to string sorting. I ported multi key sorting algorithm in my repo, but now it seems to be buggy and slower than generic insertion sort or quick sort. |
@JoeLoser how do we want to start? You will opensource the sorting package? I can add the |
There are lots of sorting algorithm which has their own strength in specific cases. I think the built-in algorithm should be the best for general cases. We can take other languages stdlib as reference. In Python, they have Timsort. In Rust, they have quicksort for unstable, timsort for stable sort. https://doc.rust-lang.org/core/slice/sort/index.html. I think it is fine for me if we start from @mzaks sorting algorithm, but I expect at the end we can have Timsort like what in Python and Rust, or other competitive sorting algorithms by default for most of the use cases. Other sorting algorithms should be a package imported if needed. |
@JoeLoser I heard that there are plans to introduce benchmarks (infrastructure and code) to std lib. Is it correct? For evolution on algorithms and data structures it would be crucial to have it, so we can make data driven decisions. |
I'll do my best to respond to everything here in one go.
|
I think it would be nice if internal code
I agree to start with Timsort, others sorting algorithm should be a standalone package for now. I believe there will come out a better SIMD-based sorting as a builtin candidate in the future 😎 |
Actually if the sorting algorithm should land in BTW after banging my head against the wall with string sorting doing something strange, I realised that the |
I just landed our basic sort algorithm from |
Review Mojo's priorities
What is your request?
I think it would be a great improvement to the stdlib to bring a
sort
/sorted
function that uses aSortable
trait for things like large length string sorting.What is your motivation for this change?
I use the
sort
function all the time in python (especially with a key) and I'd love to see this make its way into MojoAny other details?
No response
The text was updated successfully, but these errors were encountered: