C++ code for binary search using loop

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int arry[10]; int item; int n;
int loc=-1; int mid,start,end; start=0; end=9;
for(int i=0; i<10; i++)
{
cout<<“enter ur num = “;
cin>>arry[i];
}

cout<<endl<<endl;
for(int i=0; i<10; i++)
for(int j=0; j<9; j++)
if(arry[j]>arry[j+1])
{
item=arry[j];
arry[j]=arry[j+1];
arry[j+1]=item;
}

for(int i=0; i<10; i++)
{
cout<<“ur sorted arrry is = “<<arry[i]<<endl;
}
cout<<endl<<endl;

cout<<“enter num for searh = “;
cin>>n;
while(start<=end)
{
mid=(start+end)/2;

if(arry[mid]==n)
{ loc=mid;
break;
}
else
if(n<arry[mid])
end=mid-1;
else
start=mid+1;
}
cout<<endl;
if(loc==-1)

else
cout<<n<<“found at index”<<loc<<endl;

getch();
}

C++ code for binary search using recursive function

#include<iostream>
#include<conio.h>
using namespace std;
int binarysearch(int start, int end, int arry[], int n )
{ int mid;
if(start>end)
return -1;
mid=(start+end)/2;
if(arry[mid]==n)
return mid;

else
if(n<arry[mid])
{end=mid-1;
return binarysearch(start,end,arry,n);}

else
{ start=mid+1;
return binarysearch(start,end,arry,n);
}
}

void main()
{ int start=0; int end=9; int arry[10]; int item,n; int p=0;
for(int i=0; i<10; i++)
{
cout<<“enter ur num”;
cin>>arry[i];
}
for(int i=0; i<10; i++)
for(int j=0; j<9; j++)
if(arry[j]>arry[j+1])
{
item=arry[j];
arry[j]=arry[j+1];
arry[j+1]=item;
}
cout<<endl<<endl;
for(int i=0; i<10; i++)
{ cout<<“ur sorted arry is “<<arry[i]<<endl; }

cout<<endl<<endl;
cout<<“enter ur num for search”;
cin>>n;

p=binarysearch(start,end,arry,n);

if(p==-1)
else
cout<<n<<“value found in arry at index”<<p;
getch();
}

C++ code for binary search in linklist

#include<iostream>
#include<conio.h>
using namespace std;
int found=1;
class node
{int num;
node*next;
public:
node()
{}
void set_num(int n){num=n;}
int get_num(){return num;}
void set_next(node*n1){next=n1;}
node *get_next(){return next;}
};
public:

void insart(int n)
{node* nn= new node;
nn->set_num(n);

void insart_at_tail(int data)
{
node* newNode = new node;
newNode->set_num(data);
newNode->set_next(NULL);

if ( tmp != NULL )
{
while ( tmp->get_next() != NULL )
{tmp = tmp->get_next();}
tmp->set_next(newNode);}
}
void del()
cout<<“\n list is empty \n”;
else
cout<<“\n the deleting element is “<<head->get_num()<<“\n”;
delete(temp);}

}

int binary_search(int found)

{
int n=0,s=0,mid;
node *p,*ptr;
while(p!=NULL)
{n++;
p=p->get_next();
}
ptr=NULL;
while((s<=n))
mid=(s+n)/2;
for(int i=0;i<mid;i++)
{p=p->get_next();}
if(p->get_num()==found)
{ptr=p;
cout<<“FOUND\n”;
return 0;
}
else
{if(p->get_num()>found)
{n=mid-1;}
else
{s=mid+1;}
}
}

return -1;
}
void display()
cout<<“\n list is empty \n”;
else
while(temp!=NULL)
{cout<<temp->get_num()<<” “;
temp=temp->get_next();}
cout<<“\n”;
}
}
};
void main()
int c=0,n;
while (c!=5)
{cout<<” press 1 for inseart number \n”;
cout<<“press 2 for delete number \n”;
cout<<“press 3 for display numbers\n “;
cout<<“press 4 for search \n”;
cout<<“press 5 for exit \n”;
cin>>c;
switch(c)
{case 1:
int n;
cout<<“enter the number in queue \n”;cin>>n;
p.insart(n);break;
case 2:
p.del();break;
case 3:

p.display();break;
case 4:
int f,r;
cout<<“enter the number for found \n”;
cin>>f;
r=p.binary_search(f);
if(r==-1)
}
}
getch();
}

Single link listÂ Â  Â  Â  Â  Â  Â  Â  Â  StackÂ  Â Â  Â  Â  Â  Â  Â AVL TreesÂ  Â  Â  Â  Â  Â  Â Binary searchÂ Â  Â  Â  Â  Â Counting Sort

Doubly link listÂ  Â  Â  Â  Â  Â  Â  Â QueueÂ Â  Â  Â  Â  Â  Â  Â GraphsÂ  Â  Â  Â  Â  Â  Â  Â  Â  Bubble SortÂ  Â  Â  Â  Â  Â  Â  Â Radix sort

Circular link listÂ Â  Â  Â  Â  Â  Â  Â Binary search treeÂ Â  Â  Â  HashingÂ Â  Â  Â  Â  Insertion SortÂ Â  Â  Â  Â  Bucket Sort

Josephus ProblemÂ Â  Â  Â  Â  Â Tree TraversalÂ Â  Â  Â  Â  Â  Â  Â HeapsÂ Â  Â  Â  Â  Â  Â  Â  Â Quick SortÂ  Â  Â  Â  Â  Â  Â  Merge Sort

At Cui tutorial, courses, past papers and final year projects

#tutorial #cui #pastpaper #courses