Shell实现冒泡算法
#对预存的数组排序
#! /bin/bash
# filename: order
order=(5 4 3 2 1 9 8 7 6 0)
echo "Your enter is: ${order[@]}"
i=0
l=${#order[*]}
while [ $i -lt $l ]
do
j=`expr $l - 1`
while [ $j -ge $i ]
do
if [ ${order[$j]} -lt ${order[$j-1]} ] ; then
t[0]=${order[$j]}
order[$j]=${order[$j-1]}
order[$j-1]=${t[0]}
fi
j=$(($j-1))
done
i=$(($i+1))
done
echo -n "The digits after order is: ${order[*]}, "
echo Complete!
阅读(1233) | 评论(0) | 转发(0) |