ad

Binary search

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)

cout<<n<<“not found”<<endl;

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)
cout<<” value not found in array”;
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;}
};
class linklist
{node*head;
public:
linklist()
{head=NULL;}

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

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

if ( tmp != NULL )
{
while ( tmp->get_next() != NULL )
{tmp = tmp->get_next();}
tmp->set_next(newNode);}
else {head = newNode;}
}
void del()
{if(head==NULL)
cout<<“\n list is empty \n”;
else
{node*temp=head;
cout<<“\n the deleting element is “<<head->get_num()<<“\n”;
head=head->get_next();
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))
{p=head;
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()
{if(head==NULL)
cout<<“\n list is empty \n”;
else
{node*temp =head;
while(temp!=NULL)
{cout<<temp->get_num()<<” “;
temp=temp->get_next();}
cout<<“\n”;
}
}
};
void main()
{ linklist p;
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)
cout<<“no is not found \n”;
}
}
getch();
}




Related links 

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

 

Search within CuiTutorial

Scroll to Top