Monday, 15 October 2012

Practical no-12: Shell script to search an element from an araay using binay search


Practical no-12: Shell script to search an element from an araay using binay search

echo Enter array limit
read limit
echo Enter elements
n=1
while [ $n -le $limit ]
do
read num
eval arr$n=$num
n=`expr $n + 1`
done
echo Enter key element
read key
low=1
high=$n
found=0
while [ $found -eq 0 -a $high -gt $low ]
do
mid=`expr \( $low + $high \) / 2`
eval t=\$arr$mid
if [ $key -eq $t ]
then
found=1
elif [ $key -lt $t ]
then
high=`expr $mid - 1`
else
low=`expr $mid + 1`
fi
done

if [ $found -eq 0 ]
then
echo Unsuccessfull search
else
echo Successfull search
fi

OUTPUT
***********

[root@LINTEL]$ sh temporary.sh
Enter array limit3
Enter elements1
2
3
Enter key element2
Successfull search

No comments:

Post a Comment