Quicksort a nice sorting algorithm and a good example of scripting with JM°.

 

With a good readability, it looks like this:

:qsort(List l) List
	If( l.len <= 1 )
		_FUNC.return( l )
	p = l.first
	r = l.from(2)
a = _THIS.qsort( r.filter( _EACH <= p ) )
b = _THIS.qsort( r.filter( _EACH > p ) )
§ = a.add(p).concat(b)

 

A shorter version of this function looks like:

:qsort(l)
	( l.len <= 1 )
		§ = l
	p = l[1]
	r = l.from(2)
	§ = °qsort( r.filter( $$ <= p ) ).add(p).concat( °qsort( r.filter( $$ > p ) ) )

 

If you want the shortest version, here it is:

:qsort(l)
	(l.len<=1).if.go(§=l)
	§=°qsort(l.from(2).filter($$<=l[1])).add(l[1]).concat(°qsort(l.from(2).filter($$>l[1])))

 

You can test the function with this code:

tl = [16, 23, 14, 7, 21, 20, 6, 1, 17, 13, 12, 9, 3, 19]
°qsort(tl.print).print