Shell Script Array Sorting



1. Write a shell script to enter 5 numbers in one dimension array. Ask the user for: Ascending sort Descending sort

clear

printf "Enter the array size :"
read no;

for((i=0;i<$no;i++))
do
    read arr[$i];
done


for((i=0;i<$no-1;i++))
do
    for((j=$i+1;j<$no;j++))
    do
        if (( ${arr[$i]} > ${arr[$j]} ))
        then
            temp=${arr[$i]}
            arr[$i]=${arr[$j]}
            arr[$j]=$temp
        fi

    done
done
echo "Ascending Order :" ${arr[*]}

for((i=0;i<$no-1;i++))
do
    for((j=$i+1;j<$no;j++))
    do
        if (( ${arr[$i]} < ${arr[$j]} ))
        then
            temp=${arr[$i]}
            arr[$i]=${arr[$j]}
            arr[$j]=$temp
        fi

    done
done
echo "Descending Order :" ${arr[*]}


Shell Script Array Sorting Shell Script Array Sorting Reviewed by Unknown on 6:26 pm Rating: 5

No comments: