shell 冒泡
程序员文章站
2022-06-16 09:58:08
...
shell 冒泡
shell 冒泡:
``` shell
dwm=(1 2 7 8 9 0 6 5 3 2 100)
echo ${dwm[@]}
for i in `seq 0 ${#dwm[@]}`
do
for((j=i+1;j<${#dwm[@]};j++))
do
if [[ ${dwm[${i}]} -lt ${dwm[${j}]} ]];then
temp=${dwm[${i}]};
dwm[${i}]=${dwm[${j}]};
dwm[${j}]=${temp};
fi
done
done
echo ${dwm[@]}
shell 冒泡Oneline:
dwm=(1 2 7 8 9 0 6 5 3 2 100);echo ${dwm[@]};for i in `seq 0 ${#dwm[@]}`;do for((j=i+1;j<${#dwm[@]};j++));do if [[ ${dwm[${i}]} -lt ${dwm[${j}]} ]];then temp=${dwm[${i}]};dwm[${i}]=${dwm[${j}]};dwm[${j}]=${temp};fi;done;done;echo ${dwm[@]}