编程语言
首页 > 编程语言> > 3.输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

3.输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

作者:互联网

输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
[root@CentOS8 ~]# cat array_maopao_up_down.sh

!/bin/bash

********************************************************************

Author: Hbiaoming

QQ: 123456789

Date: 2022-07-14

FileName: array_maopao_up_down.sh

URL: www.haha.cn

Description: The test script

Copyright (C): 2022 All rights reserved

********************************************************************

read -p "输入数值个数:" count
declare -a num
for ((i=0;i<$count;i++));do
num[$i]=$RANDOM
done
echo "the inital array:"
echo ${num[@]}

declare -i n=$count
for ((i=0;i<n-1;i++));do
for ((j=0;j<n-1;j++));do
let x=$j+1
if (( ${num[$i]} < ${num[$x]} ));then
tmp=${num[$x]}
num[$x]=${num[$j]}
num[$j]=$tmp
fi
done
done
echo "After sort:"

image

标签:降序,echo,down,num,done,冒泡,升序,array
来源: https://www.cnblogs.com/biaoming534/p/16484332.html