From 98f20cbfa70616a1c1f1ae23740065f7420cf087 Mon Sep 17 00:00:00 2001 From: Powfu <1875065753@qq.com> Date: Wed, 27 Dec 2023 10:53:03 +0000 Subject: [PATCH] =?UTF-8?q?add=202101040022/chapter=5F9/=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=8A=98=E5=8D=8A=E6=9F=A5=E6=89=BE=E7=9A=84=E7=AE=97=E6=B3=95?= =?UTF-8?q?.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Powfu <1875065753@qq.com> --- ...6\347\232\204\347\256\227\346\263\225.cpp" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "2101040022/chapter_9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" diff --git "a/2101040022/chapter_9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" "b/2101040022/chapter_9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" new file mode 100644 index 00000000..30c108de --- /dev/null +++ "b/2101040022/chapter_9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" @@ -0,0 +1,43 @@ +#include +#define MAXL 100 //定义表中最多记录个数 +typedef int KeyType; +typedef char InfoType[10]; +typedef struct +{ + KeyType key; //KeyType为关键字的数据类型 + InfoType data; //其他数据 +} NodeType; +typedef NodeType SeqList[MAXL]; //顺序表类型 +int BinSearch(SeqList R,int n,KeyType k) //二分查找算法 +{ + int low=0,high=n-1,mid,count=0; + while (low<=high) + { + mid=(low+high)/2; + printf(" 第%d次比较:在[%d,%d]中比较元素R[%d]:%d\n",++count,low,high,mid,R[mid].key); + if (R[mid].key==k) //查找成功返回 + return mid; + if (R[mid].key>k) //继续在R[low..mid-1]中查找 + high=mid-1; + else + low=mid+1; //继续在R[mid+1..high]中查找 + } + return -1; +} +int main() +{ + SeqList R; + KeyType k=9; + int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; + for (i=0;i