Logo 
Search:

Unix / Linux / Ubuntu Articles

Submit Article
Home » Articles » Unix / Linux / Ubuntu » Homework HelpRSS Feeds

Write a shell program to search for a given number from the list of numbers provided using binary search method

Posted By: Isabel Hughes     Category: Unix / Linux / Ubuntu     Views: 10264

Write a shell program to search for a given number from the list of numbers provided using binary search method

Code for Write a shell program to search for a given number from the list of numbers provided using binary search method in Unix / Linux / Ubuntu

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
  
Share: 



Isabel Hughes
Isabel Hughes author of Write a shell program to search for a given number from the list of numbers provided using binary search method is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!