Friday, May 14, 2010

Bubble Sort Bash Shell Programming With Array And Seq Command Linux Unix

It will perform bubble sort using array function with seq command. Seq refers to sequence seq0$[no-1].
Source code bash shell Programming Algorithm
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]
doneBubble
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
BUBBLE SORT OUTPUT CS1252 Operating Systems lap
[redhat35@localhost Rhel5]$ 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

0 comments:

Flag counter

free counters