Search Here

Shell Script to sort an array of integers them in ascending order


echo -e "Enter the size of the array:\c"
read n
if (( n<1 ))
then
    echo -e "\nArray size can not be Zero or Negative"
    exit
fi
echo -e "\nEnter the array elements \n"
for (( i=1;i<=n;i++ ))
do
    read arr[$i]
done
echo -e "\nThe array elements are\n"
for (( i=1;i<=n;i++ ))
do
    echo ${arr[$i]}
done
flag=1
for (( i=1;i<=n && flag;i++ ))
do
    flag=0       
    for (( j=1;j<=n-i;j++ ))
    do       
   
        if (( arr[j]>arr[j+1] ))
        then
            (( temp=arr[j] ))
            (( arr[j]=arr[j+1] ))
            (( arr[j+1]=temp ))
            flag=1           
        fi           
    done
done
echo -e "\nThe array elements after sorting are\n"
for (( i=1;i<=n;i++ ))
do
    echo ${arr[$i]}
done

No comments:

Post a Comment