冒泡排序之shell脚本实现

学写了shell之后,想写一些简单的脚本进行熟悉shell,下面使用shell脚本写的冒泡排序

#!/usr/bin/bash
# shell 实现冒泡排序
# by 2019-5-15

echo "please input number list"
read -a arrs

for (( i=0; i<${#arrs[*]}; i=i+1 ))
do
        for (( j=0; j<${#arrs[*]}-1; j=j+1 ))
        do
                if [[ ${arrs[j]} -gt ${arrs[j+1]} ]];then
                tmp=${arrs[j]}
                arrs[j]=${arrs[j+1]}
                arrs[j+1]=${tmp}
                fi
        done
done

echo ${arrs[*]}

这是执行结果

please input number list
1 49 30 2 50 920
1 2 30 49 50 920
[root@localhost scripts]# 

 这其中参靠了 

https://blog.csdn.net/u012702547/article/details/46830239

 

posted on 2019-05-15 17:35  以后再说吧  阅读(600)  评论(0)    收藏  举报

导航