On another forum, the topic of sorting algorithms came up (someone even went as far as to implement a proper quicksort in Pure Data)… which made me wonder what a QS would look like in SC, and how it would perform.
SC has a quickSort method, and it seems to perform better than mergesort. So I wonder why we are using the slower algorithm as the default sort.
(
~bench = { |array|
"mergesort:".postln;
bench { array.copy.sort };
"quicksort:".postln;
bench { array.copy.quickSort(_ < _) };
};
)
// randomly ordered data
~bench.((1..100000).scramble);
mergesort:
time to run: 1.0105632369998 seconds.
quicksort:
time to run: 0.34835768400012 seconds.
// worst case #1: already in order
~bench.((1..100000));
mergesort:
time to run: 0.72621700100012 seconds.
quicksort:
time to run: 0.24629733899997 seconds.
// worst case #2: reverse order
~bench.((1..100000).reverse);
mergesort:
time to run: 0.8452806790001 seconds.
quicksort:
time to run: 0.26317427200001 seconds.
hjh