97 points · by fogus · 15y ago · 26 comments
https://github.com/jashkenas/coffee-script/tree/master/examp...
That's simply not true. I know for a fact that for me personally the bubble sort was the most "intuitive" algorithm because when I was in elementary school we had a computer club, and we were given the task for figuring out how to sort a list. My implementation was a bubble-sort, and at the time I had absolutely no knowledge of sorting algorithms at all.
Obviously different people will find different things "intuitive".
I actually don't like the particular iterative version of merge sort, to me it's lacking in clarity; then again, I like to use the ternary operator ?: on the left side of an assignment in javascript (as an array index or function selector), so I'm probably not the right judge when it comes to clarity ;)
I was thinking to include some sorting visualizations in my html5 canvas experiments, this looks like a neat start,thanks for posting it!
middle = Math.floor((stopIndex + startIndex)/2);Also, to anyone planning to use these in code, please note the copyright. Event though it's MIT, you still have to add the credit to your code. sigh Gotta love copyright law ;D
function mergeSort(items){
if (items.length == 1) {
return items;
}
var middle = Math.floor(items.length / 2),
left = items.slice(0, middle),
right = items.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}You don't need to run merge on the left and right partitions every call. You only need to do that if the left partition's last element is greater than the right partition's first element. Otherwise, you can just append the right partition to the left, since they are already in sorted order.