Quicksort

Monday 22 November 2004, 21:00:00 | python

Quicksort using list comprehensions:

import random

def quicksort(lst): 
     if len(lst) <= 1: return lst 
     pivot = lst[random.randrange(len(lst))]
      
     left =   [x for x in lst if x <  pivot] 
     middle = [x for x in lst if x == pivot] 
     right =  [x for x in lst if x >  pivot] 

     return quicksort(left) + middle + quicksort(right)