第十四周项目一(验证各算法)

问题及代码:

 *Copyright(c)2016,烟台大学计算机与控制工程学院 
 *All right reserved. 
 *文件名称:验证算法.cpp 
 *作者:张冰
 *时间:12月2日 
 *版本号;v1.0 
 *问题描述: 
          
  认真阅读并验证折半查找算法。 
  认真阅读并验证分块查找算法。 
  认真阅读并验证二叉排序树相关算法。  
  认真阅读并验证平衡二叉树相关算法。 
 *输入描述:无 
 *程序输出:根据要求输出 
*/  
折半
[cpp] view plain copy
#include <stdio.h>  
#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;  
    while (low<=high)  
    {  
        mid=(low+high)/2;  
        if (R[mid].key==k)      //查找成功返回  
            return mid+1;  
        if (R[mid].key>k)       //继续在R[low..mid-1]中查找  
            high=mid-1;  
        else  
            low=mid+1;          //继续在R[mid+1..high]中查找  
    }  
    return 0;  
}  
  
int main()  
{  
    int i,n=10;  
    int result;  
    SeqList R;  
    KeyType a[]= {1,3,9,12,32,41,45,62,75,77},x=75;  
    for (i=0; i<n; i++)  
        R[i].key=a[i];  
    result = BinSearch(R,n,x);  
    if(result>0)  
        printf("序列中第 %d 个是 %d\n",result, x);  
    else  
        printf("木有找到!\n");  
    return 0;  
}  
[cpp] view plain copy
递归折半  
[cpp] view plain copy
<pre name="code" class="cpp">#include <stdio.h>  
#define MAXL 100  
typedef int KeyType;  
typedef char InfoType[10];  
typedef struct  
{  
    KeyType key;                //KeyType为关键字的数据类型  
    InfoType data;              //其他数据  
} NodeType;  
typedef NodeType SeqList[MAXL];     //顺序表类型  
  
int BinSearch1(SeqList R,int low,int high,KeyType k)  
{  
    int mid;  
    if (low<=high)      //查找区间存在一个及以上元素  
    {  
        mid=(low+high)/2;  //求中间位置  
        if (R[mid].key==k) //查找成功返回其逻辑序号mid+1  
            return mid+1;  
        if (R[mid].key>k)  //在R[low..mid-1]中递归查找  
            BinSearch1(R,low,mid-1,k);  
        else              //在R[mid+1..high]中递归查找  
            BinSearch1(R,mid+1,high,k);  
    }  
    else  
        return 0;  
}  
  
int main()  
{  
    int i,n=10;  
    int result;  
    SeqList R;  
    KeyType a[]= {1,3,9,12,32,41,45,62,75,77},x=75;  
    for (i=0; i<n; i++)  
        R[i].key=a[i];  
    result = BinSearch1(R,0,n-1,x);  
    if(result>0)  
        printf("序列中第 %d 个是 %d\n",result, x);  
    else  
        printf("木有找到!\n");  
    return 0;  
}  


运行结果:

  1. #include <stdio.h>  
  2. #define MAXL 100    //数据表的最大长度  
  3. #define MAXI 20     //索引表的最大长度  
  4. typedef int KeyType;  
  5. typedef char InfoType[10];  
  6. typedef struct  
  7. {  
  8.     KeyType key;                //KeyType为关键字的数据类型  
  9.     InfoType data;              //其他数据  
  10. } NodeType;  
  11. typedef NodeType SeqList[MAXL]; //顺序表类型  
  12.   
  13. typedef struct  
  14. {  
  15.     KeyType key;            //KeyType为关键字的类型  
  16.     int link;               //指向对应块的起始下标  
  17. } IdxType;  
  18. typedef IdxType IDX[MAXI];  //索引表类型  
  19.   
  20. int IdxSearch(IDX I,int m,SeqList R,int n,KeyType k)  
  21. {  
  22.     int low=0,high=m-1,mid,i;  
  23.     int b=n/m;              //b为每块的记录个数  
  24.     while (low<=high)       //在索引表中进行二分查找,找到的位置存放在low中  
  25.     {  
  26.         mid=(low+high)/2;  
  27.         if (I[mid].key>=k)  
  28.             high=mid-1;  
  29.         else  
  30.             low=mid+1;  
  31.     }  
  32.     //应在索引表的high+1块中,再在线性表中进行顺序查找  
  33.     i=I[high+1].link;  
  34.     while (i<=I[high+1].link+b-1 && R[i].key!=k) i++;  
  35.     if (i<=I[high+1].link+b-1)  
  36.         return i+1;  
  37.     else  
  38.         return 0;  
  39. }  
  40.   
  41. int main()  
  42. {  
  43.     int i,n=25,m=5,j;  
  44.     SeqList R;  
  45.     IDX I= {{14,0},{34,5},{66,10},{85,15},{100,20}};  
  46.     KeyType a[]= {8,14,6,9,10,22,34,18,19,31,40,38,54,66,46,71,78,68,80,85,100,94,88,96,87};  
  47.     KeyType x=85;  
  48.     for (i=0; i<n; i++)  
  49.         R[i].key=a[i];  
  50.     j=IdxSearch(I,m,R,n,x);  
  51.     if (j!=0)  
  52.         printf("%d是第%d个数据\n",x,j);  
  53.     else  
  54.         printf("未找到%d\n",x);  
  55.     return 0;  
  56. }  

 

运行结果:

知识点总结:

对于折半,递归折半,分块等方法其实并不难,关键是真正能理解其降低复杂度的方法,很少的数据不能体现其简单性,数据多了就能体现了。

而对于二叉树和平衡二叉树则是更加方便的利用折半的方式将所有的数字变成二叉树的形式,平衡二叉树关键是理解哪里不平衡就改哪里

学习心得:

学习不能一蹴而就,得认真努力

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值