Wednesday, June 18, 2014

Linear Search

Example:



Algorithm:


Program:



6 comments:

  1. #include
    using namespace std;
    class searc
    {
    public:
    searc()
    {
    int DATA[10]={1,2,3,4,5,6,7,8,9,19};
    int N,LOC,K,ITEM;
    N=10;
    K=0;
    LOC=11;
    ITEM=5;
    while(LOC==11 && K<N)
    {
    if(ITEM==DATA[K])
    LOC=K;
    K=K+1;
    }
    if(LOC==11)
    cout<<"Item is not in array"<<endl;
    else
    cout<<"Item is in " << " "<<LOC<<"th"<< " "<<"position of array";

    }
    };
    int main()
    {
    searc c;
    }


    ReplyDelete
  2. // with destructor



    #include
    using namespace std;
    class searc
    {
    public:
    searc()
    {
    int DATA[10]={1,2,3,4,5,6,7,8,9,19};
    int N,LOC,K,ITEM;
    N=10;
    K=0;
    LOC=11;
    ITEM=22;
    while(LOC==11 && K<N)
    {
    if(ITEM==DATA[K])
    LOC=K;
    K=K+1;
    }
    if(LOC==11)
    cout<<"Item is not in array"<<endl;
    else
    cout<<"Item is in " << " "<<LOC<<"th"<< " "<<"position of array";

    }
    ~searc()
    {

    }
    };
    int main()
    {
    searc c;
    }



    ReplyDelete
  3. #include
    using namespace std;
    class linear
    {
    public:
    int DATA[8]={2,3,4,5,6,7,8,9},n=8,item=5,k=0,loc=-1;
    linear()
    {
    while(loc==-1 && k<n)
    {

    if(item==DATA[k])
    loc=k;
    k+=1;
    }
    if(loc==-1)
    cout<<"item is not present"<<endl;
    else
    cout<<"the locatiion of the item="<<loc<<endl;
    }
    };
    int main()
    {
    linear l;
    }

    ReplyDelete
  4. #include
    using namespace std;
    class linear
    {
    public:
    int DATA[8]={2,3,4,5,6,7,8,9},n=8,item=5,k=0,loc=-1;
    linear()
    {
    while(loc==-1 && k<n)
    {

    if(item==DATA[k])
    loc=k;
    k+=1;
    }
    if(loc==-1)
    cout<<"item is not present"<<endl;
    else
    cout<<"the locatiion of the item="<<loc<<endl;
    }
    ~linear()
    {

    }
    };
    int main()
    {
    linear l;
    }

    ReplyDelete
  5. #include
    using namespace std;
    class searc
    {
    public:
    searc()
    {
    int DATA[10]={1,2,3,4,5,6,7,8,9,19};
    int N,LOC,K,ITEM;
    N=10;
    K=0;
    LOC=11;
    ITEM=22;
    while(LOC==11 && K<N)
    {
    if(ITEM==DATA[K])
    LOC=K;
    K=K+1;
    }
    if(LOC==11)
    cout<<"Item is not in array"<<endl;
    else
    cout<<"Item is in " << " "<<LOC<<"th"<< " "<<"position of array";

    }
    ~searc()
    {

    }
    };
    int main()
    {
    searc c;
    }



    ReplyDelete
  6. #include
    using namespace std;
    class searc
    {
    public:
    int DATA[5]={1,2,3,4,5},item=2,n=5,loc=-1,k=0;
    searc()
    {
    while(loc==-1&&k<n)
    {
    if(item==DATA[k])
    loc=k;
    k=k+1;
    }
    if(loc==-1)
    cout<<"item is not present"<<endl;
    else
    cout<<"The location of item is="<<loc<<endl;
    }
    ~searc()
    {

    }
    };
    int main()
    {
    searc s;
    }


    ReplyDelete