Sorting algorithms that don’t hate you
11–20 of 84 posts
Re: Sorting algorithms that don’t hate you
#12I had way too much fun making these animations.
Re: Sorting algorithms that don’t hate you
#13Note that it is straightforward to modify an unstable sort algorithm into a stable one, when it’s okay to use O(n) extra memory: define an array that tracks each element’s original position, and when two elements otherwise compare equal, use the original position as the final tiebreaker. (With that said, Timsort must be faster in practice than this kind of retrofitted unstable sort. I’m curious how large the differen…
Re: Sorting algorithms that don’t hate you
#14An understated benefit of merge sort is that all of its access patterns have excellent locality. Regardless of big-O, this is a huge advantage because it reduces cache thrashing. Like it probably doesn't matter if you're sorting 10k entries, but when you're sorting several gigabytes it really does.
Re: Sorting algorithms that don’t hate you
#15Note that it is straightforward to modify an unstable sort algorithm into a stable one, when it’s okay to use O(n) extra memory: define an array that tracks each element’s original position, and when two elements otherwise compare equal, use the original position as the final tiebreaker. (With that said, Timsort must be faster in practice than this kind of retrofitted unstable sort. I’m curious how large the differen…
Isn't that completely unnecessary? Do a deeper comparison. If the elements are absolutely equal, then it doesn't matter.
Re: Sorting algorithms that don’t hate you
#16An understated benefit of merge sort is that all of its access patterns have excellent locality. Regardless of big-O, this is a huge advantage because it reduces cache thrashing. Like it probably doesn't matter if you're sorting 10k entries, but when you're sorting several gigabytes it really does.
In the rare event that you have to sort a big stack of paper, merge sort is also super intuitive and easy to apply by hand.
Re: Sorting algorithms that don’t hate you
#17Note that it is straightforward to modify an unstable sort algorithm into a stable one, when it’s okay to use O(n) extra memory: define an array that tracks each element’s original position, and when two elements otherwise compare equal, use the original position as the final tiebreaker. (With that said, Timsort must be faster in practice than this kind of retrofitted unstable sort. I’m curious how large the differen…
Isn't that completely unnecessary? Do a deeper comparison. If the elements are absolutely equal, then it doesn't matter.
Re: Sorting algorithms that don’t hate you
#18An understated benefit of merge sort is that all of its access patterns have excellent locality. Regardless of big-O, this is a huge advantage because it reduces cache thrashing. Like it probably doesn't matter if you're sorting 10k entries, but when you're sorting several gigabytes it really does.
In the rare event that you have to sort a big stack of paper, merge sort is also super intuitive and easy to apply by hand.
Re: Sorting algorithms that don’t hate you
#19> Recently, Timsort has become popular, also a variation of Merge Sort. However, Timsort was not invented yet in 2008. Timsort is from 2002. Not sure where this comes from. Perhaps it wasn't as popular or widely known in 2008 as it is now, but I actually learned about it at about that time. See also: https://mail.python.org/pipermail/python-dev/2002-July/02683... (edit: timsort was introduced to Java in 2009, per htt…
Re: Sorting algorithms that don’t hate you
#20Earlier quoted context omitted.
In the rare event that you have to sort a big stack of paper, merge sort is also super intuitive and easy to apply by hand.
I like to think so but I'm not sure that's true. At least I don't think I've ever seen anyone divide a stack in half, then divide the left one in half, then divide the left one of that in half, etc. as if they're doing recursive mergesort. I feel like most people start from the first card and build upward, either with insertion sort or with something like iterative mergesort.