Sunday, 5 October 2014

BINARY SEARCH

/*binary search*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int ar[10],value,size,i,flag,lower,middle,upper;
char choice;
cout<<"Enter the size ";
cin>>size;
cout<<"Enter the array\n";
for(i=1;i<=size;i++)
cin>>ar[i];
do{
flag=0;
cout<<"\nEnter search element ";
cin>>value;
lower=1;
upper=size;
while(lower<upper)
{
middle=(lower+upper)/2;
if(value>ar[middle])
lower=middle;
if(value<ar[middle])
upper=middle;

if(value==ar[middle])
{
cout<<"\nElement found ";
cout<<"Element postion "<<middle;
flag=1;
break;
}
 /*search start*/
}

LINEAR SEARCH

/*linear seacrh*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int ar[10],value,size,i,flag;
char choice;
cout<<"Enter the size ";
cin>>size;
cout<<"Enter the array\n";
for(i=1;i<=size;i++)
cin>>ar[i];
do{
flag=0;
cout<<"\nEnter search element ";
cin>>value;
 /*search start*/
for(i=1;i<=size;i++)
{
if(value==ar[i])
{
cout<<"\nElement found";
cout<<"\nElement postion ";
cout<<i;
flag=1;
}
 }
if(flag==0)
{
cout<<"\nElement not found ";     /*search end*/
}

cout<<"\nWant to continue press (1)  ";
cin>>choice;
}while(choice=='1');
}

MENU DRIVEN STACK

/*push and pop in stack*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int top=-1,size,value,stack[100],i,option;  /*variable initiation*/
char choice;
cout<<"\n Enter stack size ";
cin>>size;
do
{
cout<<"\n press 1 for push ";
cout<<"\n       2 for pop ";
cin>>option;
if(option==1)
{   if(top==size-1)
{ cout<<"\noverflow";
exit(0);
}
else
{        cout<<"\n Enter value ";
cin>>value;
top=top+1;
stack[top]=value;
cout<<"stack..................\n";
for(i=top;i>=0;i--)
cout<<stack[i]<<endl;
}
}
if(option==2)
{  if(top==-1)                                                                                           /*no element for push*/
{ cout<<"\nunderflow";
exit(0);
}
if(top==0)
{
cout<<"\n element deleted";
top=top-1;
cout<<"stack..................";
for(i=top;i>=0;i--)
cout<<stack[top]<<endl;
}

{
}

}
cout<<"\n\n\nDo u want to continue press 1 ";
cout<<"\nEnter your choice ";
cin>>choice;
}while(choice=='1');
}

PUSH IN STACK

/*push in stack*/
#include<iostream.h>
#include<conio.h>                    /*for clrscr*/
#include<stdlib.h>     /*for exit*/
void main()
{
     clrscr();
     int stack[10],top=-1,size,value;
     char choice;
     cout<<"Enter the size if element ";
     cin>>size;
do
{
  if(top==(size-1))
{  cout<<"overflow";
                   exit(0);
}
 cout<<"\nEnter the value ";
 cin>>value;
 ++top;
 stack[top]=value;
 for(choice=top;choice>=0;choice--)
 cout<<"\n\n\Your stack........... "<<stack[choice];
 cout<<endl;

cout<<"\n\n Do you want to continue press(1) ";
cin>>choice;

}while(choice=='1');
}

POP IN STACK

/*pop in stack*/
#include<iostream.h>
#include<conio.h>                    /*for clrscr*/
#include<stdlib.h>     /*for exit*/
void main()
{
clrscr();
int stack[]={1,2,3,4,5,6},top=5;
char choice;
        for(choice=top;choice>=0;choice--)
cout<<"\n\n\Your stack........... "<<stack[choice];
do
{
if(top==-1)
{  cout<<"underflow";
exit(0);
}
cout<<"\n D u want to pop element ";
cout<<"\nelement deleted ";
cout<<stack[top];
--top;
for(choice=top;choice>=0;choice--)
cout<<"\n\n\Your stack........... "<<stack[choice];
cout<<endl;
cout<<"\n\n Do you want to continue press(1) ";
cin>>choice;

}while(choice=='1');
}