Shell programming - array sorting


# Shell programming
# Sorting 5 numbers

echo "Enter 5 values : "
for i in 0 1 2 3 4
do
read a[$i]
done

echo "Entered Values are "
for (( i=0; i<5; i++))
do
echo -e "\t ${a[$i]}"
done



// Sorting loop
for ((i=0;i<4;i++))
do
for ((j=`expr $i + 1`; j<5;j++))
do
if [ ${a[$j]} -lt ${a[i]} ]
then
t=${a[$i]}
          a[$i]=${a[$j]}
          a[$j]=$t
fi
done
done

// Output
echo "Sorted Numbers : "
for ((i=0;i<5;i++))
do
echo -e "\t ${a[$i]}"
done

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu