Common Lisp
(defun qs (s)
(if s
(append
(qs (remove (car s) (cdr s) :test #'<=))
(list (car s))
(qs (remove (car s) (cdr s) :test #'>)))))
Ruby
class Array
def qs
if size > 1
self[1..-1].delete_if{|x| x >= first}.qs +
[first] +
self[1..-1].delete_if{|x| x < first}.qs
else
self
end
end
end
Matzさんご本人によるRuby版クイックソートはこちら。 Python版とHaskell版もいっしょに書かれてます。
PostScript
/car {0 get} def
/cdr {dup 1 exch length 1 sub getinterval} def
/qs {
dup length 1 gt {
1 dict begin
/x exch def
[
[ x cdr {dup x car ge {pop} if} forall ] qs aload pop
x car
[ x cdr {dup x car lt {pop} if} forall ] qs aload pop
]
end
} if
} def
Bash
某先生との共作。
remove() {
f=$1;shift
if let $#-1
then
[ $1 $f $2 ] || echo -n "$2 "
remove $f $1 ${@:3}
fi
}
qs() {
if let $#
then
local car=$1
local cdr=${@:2}
echo -n "$(qs $(remove -le $car $cdr)) "
echo -n "$car "
echo "$(qs $(remove -gt $car $cdr))"
fi
}
Smalltalk
!SequenceableCollection methodsFor: 'basic'!
rest
^self copyFrom: 2 !
!SequenceableCollection methodsFor: 'sorting'!
quickSort
self size > 1
ifTrue: [
^(self rest reject: [ :x | x >= self first ]) quickSort,
(Array with: self first),
((self rest reject: [ :x | x < self first ]) quickSort) ] ! !