BUBBLE SORT

SOURCE CODE:
echo “Enter the size of array”
read no
i=0
echo “Enter the numbers”
for i in `seq 0 $[$no – 1]`
do
read arr[i]
done
for i in `seq 0 $[$no – 1]`
do
j=0
while [ $j –lt $[$no - $i – 1]]
do
if [ $[arr[j]] –gt $[arr[$j + 1]]]
then
tmp=$[arr[$j]]
arr[$j]=$[arr[$j + 1]]
arr[$j + 1]=$tmp
fi
j=`expr $j + 1`
done
done
echo “The ordered array is……”
for i in `seq 0 $[$no – 1]`
do
echo “$[arr[$i]]”
done

OUTPUT:
[examuser35@localhost Jebastin]$ sh bubble.sh
Enter the size of array
5
Enter the numbers
100
50
87
14
25
The ordered array is……
14
25
50
87
100
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: