Design and Analysis of Algorithms
Design and Analysis of Algorithms
Program File
Submitted To:
Submitted By:
Er. Prince Verma Rajveer
Kaur
(AP)
90220370579
CSE 5th
Sem
1. /*WAP TO PERFORM LINEAR SEARCH ON
ARRAY*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n,m,v;
cout<<"enter size of array: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter"<<i+1<<"th number: ";
cin>>a[i];
}
cout<<"enter element for searching: ";
cin>>m;
for(i=0;i<n;i++)
{
if(m==a[i])
{
cout<<"element location is"<<i+1<<endl;
}
v=i;
}
if(v>=0 && v<=n)
{
cout<<"Location is found";
}
else
{
cout<<"Location not found";
}
getch();
}
OUTPUT: -
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,loc,n,m,beg,end,mid;
cout<<"Enter size of array";
cin>>n;
cout<<"Enter<<i<<th element ";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"Enter search element";
cin>>m;
beg=1;
end=n;
mid=(beg+end)/2;
while(beg<=end)
{
if(m==a[mid])
{
loc=mid;
cout<<"Location is "<<loc;
}
if(m>a[mid])
{
beg=mid+1;
mid=(beg+end)/2;
}
else
{
end=mid-1;
mid=(beg+end)/2;
}
if(m==a[mid])
{
cout<<"Location is found";
}
else
{
cout<<"Location is not found";
}
getch();
}
OUTPUT: -
#include<iostream.h>
#include<conio.h>
int tower(int n,char beg,char aux,char end);
void main()
{
clrscr();
char beg='a',end='c',aux='b';
int n;
cout<<"enter number";
cin>>n;
tower(n,beg,aux,end);
getch();
}
int tower(int n,char beg,char aux,char end)
{
if(n==1)
{
cout<<beg<<"-->"<<end<<endl;
}
else
{
tower(n-1,beg,end,aux);
cout<<beg<<"-->"<<end<<endl;
tower(n-1,aux,beg,end);
}}
OUTPUT: -
#include<iostream.h>
#include<conio.h>
void merge(int low,int mid,int high,int a[])
{
int h=low,i=low,j=mid+1;
int k,b[10];
while((h<=mid) && (j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}}
else
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i=i++;
}
for(k=low;k<=high;k++)
{
a[k]=b[k];
}
}
void mergesort(int low,int high,int a[])
{
if(low<high)
{
int mid=(low+high)/2;
mergesort(low,mid,a);
mergesort(mid+1,high,a);
merge(low,mid,high,a);
}
}
void main()
{
clrscr();
int a[10];
int i,n;
cout<<"enter size of array: ";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter"<<i<<"th element= ";
cin>>a[i];
}
mergesort(1,n,a);
cout<<"\nAfter Sorting of elements ";
for(i=1;i<=n;i++)
{
cout<<endl<<a[i];
}
getch();
}
OUTPUT: -