-
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 sorting function for memory arrays #3490
Comments
I think you're right that in-memory sorting for small bounded arrays could be acceptable and useful. Do you have use cases in mind? What sorting algorithm would you suggest? A binary heap isn't exactly sorting. It would be nice to sort in place to avoid memory expansion costs. |
I don't have a particular use case in mind, I just thought that it may be useful for somebody as OZ is the unofficial Solidity std-lib 🤷 Binary heap is one loop away from being an in-place heapsort implementation, just keep |
We can do an in place quick sort like this. One usecase we recently discovered is sorting leaves when doing a multi merkle proof verify |
That's a neat quicksort implementation! I can't wait to benchmark it against heapsort. Before I've implemented heapsort I was experimenting with quicksort too and the gas usage was much worse. My implementation could've been poor though 😅 |
🧐 Motivation
On-chain sorting is a risky design choice, but for small data sizes it could be useful for some algorithms. I'm proposing sorting of
memory
arrays, NOTstorage
arrays, that would be prohibitively expensive.📝 Details
This issue is basically a temperature check. The implementation of an in-memory binary heap already exists here, it's polished, optimized, benchmarked and tested. It only needs to be simplified and generalized for sorting
uint256
s instead of being a single-use heap for specialized user-defined types. If this issue gets a positive reception, I can do that work.The text was updated successfully, but these errors were encountered: