My little piece of the digital multiverse
Archive for December, 2011
Groovy quicksort
01 year ago
Very short post.. even my blogging style is aligning itself to the way groovy allows me to write code – i.e. quickly.
Shamelessly copied from Dierk Konig’s excellent book “Groovy in Action’, I bring you the groovy implementation of the quick sort algorithm…
def quicksort(list)
{
if ( list.size() < 2 )
return list
def pivot = list[list.size().intdiv(2)]
def left = list.findAll {item -> item < pivot}
def middle = list.findAll {item -> item == pivot}
def right = list.findAll {item -> item > pivot}
return (quicksort(left) + middle + quicksort(right))
}(For those not familiar with Groovy, findAll, closures and ranges are standard language features and this algorithm ties these concepts up so nicely…)
I’m quickly falling in love with Groovy… I can’t wait to see whats waiting for me in the Grails framework… (more on this when I have time to post)