1.输入一串数字排序
#!/bin/bash
function paixu() {
arr=($@)
len=${#arr[@]}
for ((i = 1; i < len; i++)); do
for ((j = 0; j < len - i; j++)); do
for ((m = j + 1; m < len - i + 1; m++)); do
first=${arr[$j]}
second=${arr[$m]}
if [ $first -gt $second ]; then
tmp=$first
arr[$j]=$second
arr[$m]=$tmp
fi
done
done
done
echo ${arr[@]}
}
function enter() {
read -p "请输入一组列表" num
arr=($num)
echo "原数组为:" ${arr[@]}
paixu ${arr[@]}
}
enter
2.输入一串数字排序
#!/bin/bash
function sort_num() {
read -p "请输入一串数字排序:" nums
arr=($nums)
arr1=()
while [ ${#arr[@]} -ne 0 ]; do
min=99999
index=99999
for i in ${!arr[@]}; do
if [ ${arr[$i]} -le $min ]; then
min=${arr[$i]}
index=$i
fi
done
unset arr[index]
arr1+=($min)
done
echo ${arr1[@]}
}
sort_num
标签:do,arr,20,min,len,num,done,排序,方法
From: https://www.cnblogs.com/ckh2023/p/17773568.html