[Tutor] quicksort using list comprehension

Max Noel maxnoel_fr at yahoo.fr
Sat Apr 9 22:20:54 CEST 2005


On Apr 9, 2005, at 21:50, Kent Johnson wrote:

> I think you have to return a value when len(t) <= 1. You don't return 
> anything which means you return None which can't be added to a list.
>
> Kent

	Yup. Here's a quicksort I did, adapting an example from the Wikipedia 
article on Haskell:


def qsort(toSort):
     if toSort == []:
         return []
     else:
         x = toSort[0]
         elts_lt_x = [y for y in toSort if y < x]
         elts_gt_x = [y for y in toSort if y > x]
     return qsort(elts_lt_x) + [x] + qsort(elts_gt_x)



	It is, of course, nowhere near as optimal as a "real" quicksort 
algorithm (like the one the list.sort method implements).

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"




More information about the Tutor mailing list