-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 intro sort to Array #3514
Add intro sort to Array #3514
Conversation
@c910335 WOW! Thank you so much for this! This is really useful. It will take some time for me or others to review this, but this will most probably be merged. We originally wrote a naive quicksort implementation in Crystal because we needed ir in order to implement the compiler. We knew we would eventually need to improve its performance, so your pull request is a really nice gift! I didn't know about introsort so I also learned something new today. Thank you! And, as always, it's really nice to see that idiomatic Crystal code can be competitive and even beat some C/C++ implementations :-) |
@c910335 This is awesome 🎉 ありがとうございます! |
This is nice. Java and many languages use a hybrid approach for sort to avoid the worst cases. Didn't know Crystal used qsort only so this is really a super improvement. |
i little modified your bench, there is also some worst cases:
|
Awesome work! An improvement suggestion: instead of having a duplicate with or without |
@kostya |
I found that the method "shift_median!" breaks the monotonicity of a decreasing array since it shifts the pivot for quick sort to the front while the other sorts take advantage of the monotonicity to do faster. |
I am so sorry. |
@c910335 impressive results. Fastest of the implementations most of the time, and when it's not, it's almost on par with C++? |
@c910335 |
@c910335 I've reviewed the code and also did some experiments to see if it was working correct and everything's good. Thank you so much for this!! ❤️ |
I test it on mac, the benchmark shows that the clang's sort function has better performance. |
@chenkovsky By how much? |
======== random 2097152 ============= |
Rust landed Introsort last week, too: rust-lang/rust#38192 |
@ysbaddaden Cool! Now someone should send us a pull request changing Hash's implementation to use open addressing, like Ruby 2.4.0 does :-) |
Just letting you know that Rust's not a traditional introsort. It's closer to TimSort. |
For most of the developers, performance is one of the most reasons why they choose a programming language.
But how do they know the performance of a programming language?
Usually, they test it by sorting an array because sorting is the most basic, classic and the most important algorithm in the world.
That is what I did these days.
After several tests, I found that Array#sort is usually faster than qsort in C but sometimes slower than std::sort in C++.
However, I believe that Crystal can do better.
Therefore, let me introduce "introspective sort" a.k.a. introsort, the fastest sorting algorithm as far as I know.
Introsort - Wikipedia
According to Wikipedia, Introsort is a hybrid sorting algorithm that consists of quicksort, heapsort and insertion sort.
It combines the good parts of the three algorithms, with fast average performance (quicksort), worst-case O(n log n) runtime (heapsort) and fastest practical performance on an almost sorted array (insertion sort).
Benchmarks