So I was looking up sorting algorithms on wikipedia and found Cocktail Sort to be quite interesting. It's the same as bubble sort, only it makes passes in both directions, essentially cutting the sorting time in a little less than half. So if it took bubble sort 10 seconds, cocktail sort would take about 6 seconds.
My question is, how would cocktail sort be programmed in DM? I'm not familiar with the language being used on the wikipedia page :\
ID:273781
Apr 10 2011, 5:19 am
|
|
In response to Zaoshi
|
|
Thanks for clarifying for me :)
proc/cocktail_sort(list/L) proc/cocktail_sort2(list/L) |
First code is just basic sorting:
Second adds little optimization by not sorting already sorted elements:
I nearly directly rewrote code, there's chance I got indexes wrong, or etc, but it seems to sort correctly.
Second one seems to be about 25% faster than first one.
Code I used for stress test: