实验四 查找

实验四查找

一、   实验目的

1.掌握顺序查找、二分法查找的算法。

2.能运用线性表的查找方法解决实际问题。

二、   实验内容

1.实现一个在无序线性表中查找元素x的算法

2.给出在一个递增有序表A中采用二分查找算法查找值为x的元素的算法。

3. 创建一棵二叉搜索树,给出查找元素x的算法

4. 自行设计测试步骤,并在实验步骤里详细说明要测试操作的边界情况。

5. 比较以上三种算法的时间复杂度。

 

 

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

typedef long clock_t;

int count=0;

 

template <class T>

struct BSTNode{//二叉树结点类

       T data;

       BSTNode<T>   *left,*right;

       BSTNode():left(NULL),right(NULL){}

       BSTNode(const Td,BSTNode<T>*l=NULL,BSTNode<T>*r=NULL):

       data(d),left(l),right(r){}

    ~BSTNode(){}

       void setdata(Tt){data=t;}

       T getdata(){returndata;}

};

 

 

template <class T>

class BST{

       public:

              BST():root(NULL){}

              BST(T vvalue);

              ~BST(){}

           bool search(const T& x)const

              {

                     return(search(x,root)!=NULL)?true:false;

              }

       

              bool insert(constT& el){return insert(el,root);}

             

       private:

              BSTNode<T>*root;

              T Refvalue;  //输入停止标志,用于输入

              int search(constT x,BSTNode<T>*ptr)const;  //递归:搜索

           bool insert(const T&el,BSTNode<T> *& ptr);//递归:插入

         

};

 

template<class T>

int BST<T>::search(const T x,BSTNode<T>*ptr)const

{

       if(ptr==NULL){cout<<"失败"<<endl;return 0;}

       BSTNode<T>*temp=ptr;

       while(temp!=NULL)

       {

              if(x==temp->data)

              {

                     cout<<temp->data;

                     count++;

                     cout<<"搜索成功"<<endl;

                     break;

              }

           else if(x<temp->data)

              {

                     cout<<temp->data;

                     count++;

                     cout<<"失败"<<endl;

                     temp=temp->left;

              }

           else if(x>temp->data)

              {

                     cout<<temp->data;

                     count++;

                     cout<<"搜索成功"<<endl;

                     temp=temp->right;

              }

       }

       return count;

};

 

template <class T>

bool BST<T>::insert(const T&el,BSTNode<T>*&ptr){

       if(ptr==NULL){

              ptr=newBSTNode<T>(el);//创建新结点

              if(ptr==NULL){cerr<<"Outof Space"<<endl; exit(1);}

              return true;

       }

       elseif(el<ptr->data) insert(el,ptr->left); //左子树插入

            else if(el>ptr->data)insert(el,ptr->right); //右子树插入

                    else return false;     //el已在树中,无需插入

}

 

template <class T>

BST<T>::BST(T vvalue){//建立二叉树

        T t;root=NULL;Refvalue=vvalue;//置空树,refvalue 为一个输入结束标志

              cin>>t;

              while(t!=Refvalue){

                     insert(t,root);cin>>t;  //插入,再输入数据

              }

}

 

void main(void){

 

       //cout<<"建立二叉搜索树:(-1为结束标志)"<<endl;

       BST<int>  bst; //停止输入标志为-1

       int x,c;

       int number;

       clock_t start,finish;

       double duration;

       srand((unsigned)time(0));

        for(int i=1;i<=100;i++){

               x=rand()%100;

              bst.insert(x);

        }

        cout.width(40);

        cout<<"二叉树搜索"<<endl;

        cout<<"输入要查找数(100以内)"<<endl;

        cin>>number;

        start=clock();

        c=bst.search(number);

        finish=clock();

        duration=(double)(finish-start)/CLOCKS_PER_SEC;

        cout<<"共查找"<<count<<""<<endl;

        cout<<duration<<'s'<<endl;

      

}

 

 

 

 

 

#include <stdio.h>

#include <iostream.h>

#include<math.h>

#include<time.h>

#define N 21

void main(void)

{

 int a[N];

 int i,n,num;

 inttop,bottom,mid;

 int flag=1; //如果在表列中找到数字,则值为1,否则为0

 int loc=-1;//要查找的数在表列中的位置,如果loca=-1表示表列中没有这个数;如果有这个数,则它的值为所在的位置

 clock_tstart,end;

 double time;

 printf("你想在多少个数中进行折半查找,请输入(1--20):");

 scanf("%d",&n);

 

 while(n<1 ||n>20)

 {

  printf("你输入的数不正确,请重新输入。\n");

  printf("你想在多少个数中进行折半查找,请输入(1--20):");

 scanf("%d",&n);

 }

 

 printf("请你输入一个整数 a[1]:");

 scanf("%d",&a[1]);

 

 i=2;

 printf("从小到大输入列表\n");

 while(i<=n)   //输入从小到大的表列

 {

  printf("请你输入一个整数 a[%d]:",i);

  scanf("%d",&a[i]);

  if(a[i] >a[i-1])

   i++;

  else

   printf("你输入的数不满足要求,请重新输入。\n");

 }

 

 //输出表列

 printf("\n输出表列\n");

 for(i=1; i<=n;i++)

 {

 printf("%6d",a[i]);

 }

 printf("\n");

 

 printf("请你输入要查找的数:\n");

 scanf("%d",&num);

 

 flag=1; //假设输入的数在表列中

 

 top=n;

 bottom=1;

 mid=(top+bottom)/2;

 

 

 start=clock();

 

 while(flag) 

 {

 printf("top=%d, bottom=%d, mid=%d,a[%d]=%d\n",top,bottom,mid,mid,a[mid]);

  if((num>a[top]) || (num<a[bottom]) ) //输入的数num>a[top] 或者num<a[bottom],肯定num不在这个表列中

  {

   loc=-1;

   flag=0;

  }

  elseif(a[mid]==num)  //如果num 等于找到的数

  {

   loc=mid;

   printf("找到数 %6d 的位置%2d\n",num,loc);

   break;

  }

  elseif(a[mid]>num)  // a[mid]>num,则num 一定在 a[bottom]a[mid-1]范围之内

  {

   top=mid-1;

  mid=(top+bottom)/2;

  }

  elseif(a[mid]<num) // a[mid]<num,则num 一定在 a[mid+1]a[top]范围之内

  {

   bottom=mid+1;

  mid=(top+bottom)/2;

  }

 }

 end=clock();

 if(loc==-1)

 {

  printf("%d 这个数在表列中没有找到。\n",num);

 }

 time=(double)(end-start)/CLOCKS_PER_SEC;

 cout<<"这半查找时间"<<time<<endl;

}

  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值