Live data from Hacker News

Quicksort in 3 lines of shell

github.com

1–10 of 20 posts

Re: Quicksort in 3 lines of shell

#6
A couple of characters less in C ( I didn't count whitespace in both ):

void qs( int* a , int z )

{

if(z > 1){ ; int p = a[z/2], s = 0, e = z-1, t; while(s for(;a[s] p; e--); if(s a[s++] = a[e]; a[e--] = t; } } qs( a , e+1 ); qs( a+s , z-s ); }

}

Re: Quicksort in 3 lines of shell

#8
Let me fix that for you.

    qsort(){
        local L="";
        local G="";
        [ $# -eq 1 ] && echo $1 && return;
        P=$1; 
        shift; 
        for i in $@; do 
            [ $i -lt $P ] && L="$L $i" || G="$G $i"; 
        done
        [ -z "$L" ] || L=`qsort $L`; 
        [ -z "$G" ] || G=`qsort $G`; 
        echo "$L $P $G"
    }
    qsort $@
Looks more like 14 lines to me.
Post reply on HN