Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
Line 446: Line 446:


qsort() {
qsort() {
lc=`mktemp -p. -t chan.XXXX`
lc="1.$1"
gc=`mktemp -p. -t chan.XXXX`
gc="2.$1"
(
(
read pivot; while read n
read pivot;
while read n
do
do
expr $pivot - $n |(read x;case $x in
test $pivot -gt $n && echo $n >> $lc || echo $n >> $gc
-*) echo $n >> $lc ;;
*) echo $n >> $gc ;;
esac)
done
done
test "$pivot" != '' && ( (cat -s $lc | qsort $lc ); echo $pivot; (cat -s $gc | qsort $gc ))
case $pivot in
[0-9]) (cat $lc | qsort ); echo $pivot; (cat $gc | qsort );;
rm -f $lc $gc;
esac
rm $lc $gc;
)
)
}
}



# using it.
cat to.sort | qsort
cat to.sort | qsort