I have the following function in a bash script that fails to return the sorted array. I think the problem lies in the recursion not correctly passing the arrays, but I can't tell what I'm doing wrong. Anyone see the problem?
function quicksort () {
local array=( `echo "$1"` )
local -a l
local -a g
local x=
if [ ${#array[@]} -lt 2 ]; then
echo ${array[@]}
else
local pivot=${array[0]}
for x in ${array[@]}; do
echo "$x, $pivot"
if [ $x -lt $pivot ]; then
l=( ${l[@]} $x )
else
g=( ${g[@]} $x )
fi
done
echo `quicksort "$l"` $pivot `quicksort "$g"`
fi
}
Yes, I'm using bash. I tried the POSIX version as well, to see if it made any difference.
$1 should contain the array passed to the function. I found this method searching the web. Is there a better method?
I suspect the problem may be related to this since when it descends to the first recursion level, the array length is seen as 1 and the function exits, returning only three numbers from the array. A check of $l and $g just prior to passing them to the recursive call shows them as they should be, so I'm not sure what the problem is.
The elements. I tried the code you suggested and I get a "bad substitution" error. The original one I used does populate the "array" variable correctly, however. At least on the first pass.
I figured it out.
I had a logic error in the partitioning loop that would send one of the branches into an endless loop. :o\ Using '*' instead of '@' worked also, although I don't fully understand why yet. Here's my final code and thanks for all your help. I learned a lot, which was the point after all.
quicksort () {
local -a array=( $1 )
local -a l
local -a g
if [ ${#array[@]} -lt 2 ]; then
echo ${array[@]}
else
pivot=${array[0]}
for x in ${array[@]}; do
if [ $x -lt $pivot ]; then
l=( ${l[@]} $x )
elif [ $x -gt $pivot ]; then
g=( ${g[@]} $x )
fi
done
echo `quicksort "${l[*]}"` $pivot `quicksort "${g[*]}"`
fi
}