# !/bin/bash
read -p "请输入" key
a=(9 8 7 6 5 4 3 2 1)
lt=0
rt=8
for (( ;lt<=rt; ))
do
mid=$(((lt+rt)/2))
if test ${a[mid]} -eq $key
then
echo "数组中存在$key"
break
elif
test ${a[mid]} -gt $key
then
lt=$((mid+1)
else
rt=$((mid-1))
fi
done
if test $lt -gt $rt
then
echo "数组中不存在$key"
fi
shell 二分法
最新推荐文章于 2022-07-05 13:03:40 发布