r/programming • • Dec 02 '19

Bubble sort visualization

7.4k Upvotes

268 comments sorted by

View all comments

Show parent comments

2

u/caltheon Dec 03 '19

There is little reason to ever implement your own sorting algorithm aside from learning.

6

u/SirClueless Dec 03 '19

That's true, but there are definitely cases where you will need to choose between, say, sort() and stable_sort(). Or where it's worth architecting your system so it doesn't need to call sort().

Knowing what sorting algorithms do and about how fast they are is useful info. And the best way to learn is to implement them yourself at least once, perhaps in some kind of algorithms class or something, even if you never plan on doing it ever again. Calling your library's sort because it would take you a few hours to implement well and risks containing bugs or corner cases is just a smart use of your valuable time. Calling your library's sort because you're a shoddy engineer who couldn't implement it yourself if you tried is sad.

3

u/Dworgi Dec 03 '19

I think everyone should implement their own containers every once in a while. I recently did, and it was pretty illuminating. The corner cases really bite you in the ass. It's good practice. Ditto for sorting.

Quicksort on small data sizes (ie. ones where cache latency is significant) can actually be rather terrible. Wouldn't know that if I hadn't implemented it for practice.

1

u/[deleted] Dec 03 '19

AFAIK no library heapifies the data before selection sorting it. But it seems to perform faster.

https://youtu.be/FJJTYQYB1JQ