SCRIPT TO FIND THE KEY IN BINARY SEARCH
#!/bash/sh
echo "enter the limit"
read lim
echo " enter the element"
n=1
while [ $n -le $lim ]
do
read num
eval arr$n=$num
n=`expr $n + 1`
done
echo "enter the key element"
read k
l=1
h=$n
found=0
while [ $found -eq 0 -a $h -ge $l ]
do
mid=`expr \( $l + $h \) / 2`
eval t=\$arr$mid
if [ $k -eq $t ]
then
found=1
elif [ $k -lt $t ]
then
h=`expr $mid - 1`
else
l=`expr $mid + 1`
fi
done
if [ $found -eq 0 ]
then
echo " key not found"
else
echo "key found"
fi
#!/bash/sh
echo "enter the limit"
read lim
echo " enter the element"
n=1
while [ $n -le $lim ]
do
read num
eval arr$n=$num
n=`expr $n + 1`
done
echo "enter the key element"
read k
l=1
h=$n
found=0
while [ $found -eq 0 -a $h -ge $l ]
do
mid=`expr \( $l + $h \) / 2`
eval t=\$arr$mid
if [ $k -eq $t ]
then
found=1
elif [ $k -lt $t ]
then
h=`expr $mid - 1`
else
l=`expr $mid + 1`
fi
done
if [ $found -eq 0 ]
then
echo " key not found"
else
echo "key found"
fi