0% found this document useful (0 votes)
8 views5 pages

Algoassignment 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Algoassignment 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<iostream>

#include<algorithm>

using namespace std;

class SearchingAlgo{

public:

int* arr;

int size;

SearchingAlgo(int A[],int n){

arr=new int[n];

size=n;

int linearsearchiterative(int A[],int x){

for(int i=0;i<size;i++){

if(A[i]==x){

return i;

return -1;

int linearsearchrecursive(int A[],int x,int i,int j ){

if(i>j){

return -1;

}
if(A[i]==x){

return i;

return linearsearchrecursive(A,x,i+1,j);

int binarysearchiterative(int A[],int x){

int l=0;

int h=size-1;

sort(A,A+size);

while(l<=h){

int mid=(l+h)/2;

if(A[mid]==x){

return mid;

else if(x<A[mid]) h=mid-1;

else l=mid+1;

return -1;

}
int binarysearchrecursive(int A[],int x,int l,int h){

sort(A,A+size);

if(l>h){

return -1;

int mid=(l+h)/2;

if(A[mid]==x){

return mid;

else if(x<A[mid]){

return binarysearchrecursive(A,x,l,mid-1);

else{

return binarysearchrecursive(A,x,mid+1,h);

};

int main(){

int x;

cout<<"enter the number you want to search:";


cin>>x;

int A[]={10,1 ,34, 18 , 56,101,23};

int n=sizeof(A)/sizeof(A[0]);

SearchingAlgo s(A,n);

int itelisea=s.linearsearchiterative(A,x);

if(itelisea!=-1){

cout<<"key found at index:"<<itelisea<<endl;

else {

cout<<"not found";

// int itebisearch=s.binarysearchiterative(A,x);

// if(itebisearch!=-1){

// cout<<"key found at index:"<<itebisearch<<endl;

// }

// else{

// cout<<"key not found!";

// }

// int reclisear=s.linearsearchrecursive(x,0,n-1);

// if(reclisear!=-1){

// cout<<"key foundrec at:"<<reclisear<<endl;


// }

// else{

// cout<<"not found";

// }

// int birecse=s.binarysearchrecursive(x,0,n-1);

// if(birecse!=-1){

// cout<<"elemnt found:"<<birecse<<endl;

// }

// else{

// cout<<"not found";

// }

return 0;

You might also like