0% found this document useful (0 votes)
65 views

Design and Analysis of Algorithms

This document contains 5 C++ programs implementing different sorting and searching algorithms: 1) Linear search on an array 2) Binary search on an array 3) Solution to the Tower of Hanoi problem 4) Merge sort on an array 5) Quick sort on an array Each program is presented with its code and an output section, though the output sections are blank. The programs are submitted by Rajveer Kaur as part of a Design and Analysis of Algorithms program file.

Uploaded by

Rajveer Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Design and Analysis of Algorithms

This document contains 5 C++ programs implementing different sorting and searching algorithms: 1) Linear search on an array 2) Binary search on an array 3) Solution to the Tower of Hanoi problem 4) Merge sort on an array 5) Quick sort on an array Each program is presented with its code and an output section, though the output sections are blank. The programs are submitted by Rajveer Kaur as part of a Design and Analysis of Algorithms program file.

Uploaded by

Rajveer Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

CTIEMT,Shahpur DAA Program File

Design And Analysis


of Algorithms

Program File

Submitted To:
Submitted By:
Er. Prince Verma Rajveer
Kaur

Rajveer Kaur 1 90220370579


CTIEMT,Shahpur DAA Program File

(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: ";

Rajveer Kaur 2 90220370579


CTIEMT,Shahpur DAA Program File

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();
}

Rajveer Kaur 3 90220370579


CTIEMT,Shahpur DAA Program File

OUTPUT: -

Rajveer Kaur 4 90220370579


CTIEMT,Shahpur DAA Program File

2. /* WAP TO PERFORM BINARY SEARCH


ON ARRAY*/

#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;

Rajveer Kaur 5 90220370579


CTIEMT,Shahpur DAA Program File

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;
}

Rajveer Kaur 6 90220370579


CTIEMT,Shahpur DAA Program File

if(m==a[mid])
{
cout<<"Location is found";
}
else
{
cout<<"Location is not found";
}
getch();
}

Rajveer Kaur 7 90220370579


CTIEMT,Shahpur DAA Program File

OUTPUT: -

Rajveer Kaur 8 90220370579


CTIEMT,Shahpur DAA Program File

3. /*WAP TO SOLVE THE TOWER OF HANOI


PROBLEM */

#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);
}}

Rajveer Kaur 9 90220370579


CTIEMT,Shahpur DAA Program File

OUTPUT: -

Rajveer Kaur 10 90220370579


CTIEMT,Shahpur DAA Program File

4. /* WAP TO SORT THE ELEMENT OF


GIVEN ARRAY LISTBY MERGE SORT*/

#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++;

Rajveer Kaur 11 90220370579


CTIEMT,Shahpur DAA Program File

}}
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++)

Rajveer Kaur 12 90220370579


CTIEMT,Shahpur DAA Program File

{
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();
}

Rajveer Kaur 13 90220370579


CTIEMT,Shahpur DAA Program File

OUTPUT: -

Rajveer Kaur 14 90220370579


CTIEMT,Shahpur DAA Program File

5. /* WAP TO SORT THE ELEMENT OF


GIVEN ARRAY LIST BY QUICK SORT*/

Rajveer Kaur 15 90220370579

You might also like