Bookmark this on Hatena Bookmark
Hatena Bookmark - Groovy quicksort
Share on Facebook
Post to Google Buzz
Bookmark this on Yahoo Bookmark
Bookmark this on Livedoor Clip
Share on FriendFeed
Groovy quicksortpeter mares dot com

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)

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks