Shell 编程--数组冒泡排序

[root@localhost ~]# vim maopao.sh 

#!/bin/bash
array=(3 6 7 4 2)

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

echo ${array[*]}
[root@localhost ~]# chmod +x maopao.sh 
[root@localhost ~]# ./maopao.sh 
7 6 4 3 2

猜你喜欢

转载自blog.csdn.net/weixin_48191211/article/details/108182760
今日推荐