Now with Scratch 1.3 out, and lists in many projects, let's start checking out and collecting sorting strategies and algorithms that work in Scratch:
Everybody is welcome to post your Sorting Projects to the Sorting Gallery
Let's explore new ways to sort those lists!
Offline
archmage's bubble sort project http://scratch.mit.edu/projects/archmage/257741 see,s to work
Offline
Achmage's bubble sort project is very nice, and so are some others that I have added to the Sorting Gallery. There are even some which are pretty old but are great illustrations of underlying ideas and methods.
Now the interesting question is: How can you speed up sorting in Scratch to a point where lists with more than - say - ten items ( how about 100?) become useful?
I was suprised that the relatively simple Insertion Sort algorithm works so well in Scratch, so that probably becomes my favorite one. Problem is, it doesn't rearrange the original list, so in many contexts you'll have to copy the "sorted list" back to the original one to get the results where you need them, which is going to slow down the whole process again.
There are also some strategies you can employ to make sorting faster, the most powerful one of course being parallelism. The tricky part is how to divide an unsorted list into several ones, i.e. how to find a passable pivot value. I have experimented with just using the list's plain average value. This works fine for evenly distributed lists, but only there. For others, the median would probably work better. Now here's a challenge to all: How can you find out the median of the values in a list in Scratch without sorting it first ?
Offline
Also, for lists that are already mostly sorted (for example, if you were using a list to store the order for drawing various layers having different z-depths, which might change order slowly) a cocktail sort seems to work fairly well. It is basically a bubble sort that goes top-down at the same time it goes from the bottom up.
Offline
Isn't it fun how you can tweak sorting algorithms and actually experience performance gains, because Scratch is so slow that you'll notice big differences? Try beating this: http://scratch.mit.edu/projects/Jens/262100
Offline