how to a slightly shorter quick sort?

i found a simple article about quicksort in python and ruby. So, it was bestly compressed in whole part.


in python:
q=lambda s:s if len(s)<2 else q([x for x in s[1:]if x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]])
(source: this link)

in ruby:
def q(s);t=s.dup;t.size<=1 ? t :(p=t[i=t.size/2];t.delete_at(i);x,y=[],[];t.each{|e|e<p ? x<<e : y<<e};n=q(x);m=q(y);n+[p]+m)end

(source: this link)

An animation of the quicksort algorithm sortin...Image via Wikipedia

No comments: