Professional Documents
Culture Documents
Aim: Implement Heap Sort for the following data types int, float, double using Templates
#include<iostream>
#include<conio.h>
using namespace std;
template<class T>
void heapsort(T a[],int n)
{
int x;
for(int i=1;i<=n;i++)
{
insert(a,i);
}
for(int i=n;i>=1;i--)
{
delmax(a,i,x);
a[i]=x;
}
}
template<class T>
int insert(T a[],int n)
{
int i=n;
int item=a[n];
while((i>1)&&(a[i/2]<item))
{
a[i]=a[i/2];
i=i/2;
}
a[i]=item;
return 1;
}
template<class T>
int delmax(T a[],int n,int &x)
{
if(n==0)
{
cout<<"heap is empty:";
return 0;
}
x=a[1];
a[1]=a[n];
adjust(a,1,n-1);
return 1;
}
template<class T>
void adjust(T a[],int i,int n)
{
int j=2*i;
int item=a[i];
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
{
j++;
}
if(item>a[j])
{
break;
}
a[j/2]=a[j];
j=2*j;
}
a[j/2]=item;
}
int main()
{
int a[20],n;
cout<<"\nenter the value :";
cin>>n;
cout<<"\n enter the elements:";
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\nbefore sorting:";
for(int i=1;i<=n;i++)
{
cout<<a[i]<<"\t";
}
heapsort(a,n);
cout<<"\n";
cout<<"\n elements after sorting:";
for(int i=1;i<=n;i++)
{
cout<<a[i]<<"\t";
}
_getch();
return 0;
}
Output:
Experiment-2
Aim: Implement Divide & Conquer technique to sort the following data
4 ,62,721,324,426,511,32,61,90
#include <iostream>
#include <conio.h>
using namespace std;
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
int getcount( ) ;
static int split ( int *, int, int ) ;
void quiksort ( int lower, int upper ) ;
void display( ) ;
};
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
int main( )
{
array a ;
a.add ( 4 ) ;
a.add ( 62 ) ;
a.add ( 721 ) ;
a.add ( 324 ) ;
a.add ( 426 ) ;
a.add ( 511 ) ;
a.add ( 32 ) ;
a.add ( 61 ) ;
a.add ( 90 ) ;
cout << "\nQuik sort.\n" ;
cout << "\nArray before sorting:" << endl ;
a.display( ) ;
int c = a.getcount( ) ;
a.quiksort ( 0, c - 1 ) ;
cout << "\nArray after quick sorting:"<< endl ;
a.display( ) ;
getch();
return 0;
}
Output :
Experiment-3
Aim: Write a program to implement the following Sorting Techniques
Bubble Sort
#include <iostream>
#include <conio.h>
#define MAX 10
using namespace std;
class bubsort{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};
Insertion Sort
#include <iostream>
#include <conio.h>
#define MAX 10
using namespace std;
class Inssort{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};
int main()
{
cout<<"\n*****Insertion Sort*****\n";
Inssort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
getch();
}
Output:
Quick Sort
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
void srt(int[],int,int);
int a[10],count=0,n;
cout<<"Ener 10 values in unsorted order : \n";
for (n=0;n<10;n++)
{
cout<<"value no.: "<<(n+1)<<"\t";
cin>>a[n];
count++;
}
n=0;
srt(a,n,count-1);
cout<<"\t\tThe Sorted order is : \n";
for (n=0;n<10;n++)
{
cout<<"\t\tposition : "<<(n+1)<<"\t"<<a[n]<<"\n";
}
getch();
return 0;
}
void srt(int k[20],int lb,int ub)
{
int i,j,key,flag=0,temp;
if (lb<ub)
{
i=lb;
j=ub+1;
key=k[i];
while(flag!=1)
{
i++;
while(k[i]<key)
{
i++;
}
j--;
while(k[j]>key)
{
j--;
}
if (i<j)
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
}
else
{
flag=1;
temp=k[lb];
k[lb]=k[j];
k[j]=temp;
}
}
srt(k,lb,j-1);
srt(k,j+1,ub);
}
}
Output:
Heap Sort
#include<iostream>
#include<conio.h>
using namespace std;
template<class T>
void heapsort(T a[],int n)
{
int x;
for(int i=1;i<=n;i++)
{
insert(a,i);
}
for(int i=n;i>=1;i--)
{
delmax(a,i,x);
a[i]=x;
}
}
template<class T>
int insert(T a[],int n)
{
int i=n;
int item=a[n];
while((i>1)&&(a[i/2]<item))
{
a[i]=a[i/2];
i=i/2;
}
a[i]=item;
return 1;
}
template<class T>
int delmax(T a[],int n,int &x)
{
if(n==0)
{
cout<<"heap is empty:";
return 0;
}
x=a[1];
a[1]=a[n];
adjust(a,1,n-1);
return 1;
}
template<class T>
void adjust(T a[],int i,int n)
{
int j=2*i;
int item=a[i];
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
{
j++;
}
if(item>a[j])
{
break;
}
a[j/2]=a[j];
j=2*j;
}
a[j/2]=item;
}
int main()
{
int a[20],n;
cout<<"\nenter the total no of elements :";
cin>>n;
cout<<"\n enter the elements:";
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\nbefore sorting \n\t\t:";
for(int i=1;i<=n;i++)
{
cout<<a[i]<<"\t";
}
heapsort(a,n);
cout<<"\n";
cout<<"\n elements after sorting\n\t\t:";
for(int i=1;i<=n;i++)
{
cout<<a[i]<<"\t";
}
_getch();
return 0;
}
Output:
Radix Sort
#include <iostream>
#include <conio.h>
#define MAX 10
using namespace std;
class radixsort{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};
largest=arr[0];
divisor = divisor*10;
}
}
int main()
{
cout<<"\n*****Radix Sort*****\n";
radixsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
getch();
}
Output:
Merge Sort
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr1[10],arr2[10],result[20],i;
cout<<"Enter 10 numbers [arr1]: ";
for(i=0;i<10;i++)
cin>>arr1[i];
cout<<"\nEnter 10 numbers [arr2]: ";
for(i=0;i<10;i++)
cin>>arr2[i];
int item=mrg_sort(arr1,10,arr2,10,result);
cout<<"\n\nAfter sort : ";
for(i=0;i<item;i++)
cout<<result[i]<<" ";
getch();
return 0;
}
Output:
Merge Sort
Enter 10 numbers [arr1] : 45
85
96
35
75
48
31
20
05
10
Enter 10 Numbers [arr2]: 67
79
11
19
25
33
74
68
55
44
Sorted Array: 05 10 11 19 20 25 31 33 35 44 45 55 67
68 74 75 79 85 96
Experiment-4
Aim: Write a program to implement expression tree traversal
Infix
Prefix
Postfix
#include <iostream>
#include <conio.h>
#include<cstdio>
using namespace std;
struct node{
char data;
node *left;
node *right;
};
char postfix[35];
int top=-1;
node *arr[35];
//it is used for inseting an single element in//a tree, i.e. is pushing of single element.
void push(node *tree){
top++;
arr[top]=tree;
}
node *pop(){
top--;
return(arr[top+1]);
}
cout<<"*****Expression Tree*****\n";
cout<<"Enter Postfix Expression : ";
cin>>postfix;
Output:
Experiment-5
Aim: Implement any greedy algorithm to evaluate minimum weight spanning tree
#include <iostream>
#include <conio.h>
#define ROW 7
#define COL 7
#define infi 5000 //infi for infinity
using namespace std;
class prims
{
int graph[ROW][COL],nodes;
public:
prims();
void createGraph();
void primsAlgo();
};
prims :: prims(){
for(int i=0;i<ROW;i++)
for(int j=0;j<COL;j++)
graph[i][j]=0;
}
for(i=0;i<nodes;i++)
selected[i]=flse;
selected[0]=tre;
ne=0;
for(i=0;i<nodes;i++)
{
if(selected[i]==tre){
for(j=0;j<nodes;j++){
if(selected[j]==flse){
if(min > graph[i][j])
{
min=graph[i][j];
x=i;
y=j;
}
}
}
}
}
selected[y]=tre;
cout<<"\n"<<x+1<<" --> "<<y+1;
ne=ne+1;
}
}
int main(){
prims MST;
cout<<"\nPrims Algorithm to find Minimum Spanning Tree\n";
MST.createGraph();
MST.primsAlgo();
getch();}
Output:
Experiment-6
Aim: Write a program to implement Stack with virtual function.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class base
{
public:
base()
{}
virtual void push(int x)
{
}
virtual void pop()
{
}
virtual void display()
{
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice : ";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
Output:
Experiment-7
Aim: Write a program for ‘Plus’ and ‘Minus’ Operator Overloading.
#include<iostream>
#include<conio.h>
using namespace std;
class vector
{
//Visit c-madeeasy.blogspot.com
public:
int x,y,z;
void read()
{
cout<<"\n\nEnter the magnitude of i : ";
cin>>x;
cout<<"\n\nEnter the magnitude of j : ";
cin>>y;
cout<<"\n\nEnter the magnitude of k : ";
cin>>z;
}
vector operator -()
{
x=-x;
y=-y;
z=-z;
}
vector operator +(vector b)
{
vector c;
c.x=x+b.x;
c.y=y+b.y;
c.z=z+b.z;
return c;
}
void display()
{
cout<<x<<"i"<<"+"<<y<<"j"<<"+"<<z<<"k";
}
};
int main()
{
vector v1,v2,v3;
int choice,cont;
do
{
cout<<"\n\tVECTORS\n\n1.Negation of The Vector\n\n2.Sum of Vecors\n\n";
cout<<"\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1 : cout<<"\nEnter the Vector\n";
v1.read();
-v1;
cout<<"Negation of the Vector : ";
cout<<v1.x<<"i"<<v1.y<<"j"<<v1.z<<"k";
break;
case 2 : cout<<"\n\nEnter the First Vector : ";
v1.read();
cout<<"\n\nEnter the Second Vector : ";
v2.read();
v3=v1+v2;
cout<<"\n\nThe Sum of Vectors : ";
v3.display();
break;
default : cout<<"\n\nInvalid Choice";
}
cout<<"\n\n\nDo you want to continue?(1-YES,0-NO)\n";
cin>>cont;
}while(cont==1);
getch();
return 0;
}
Output:
Experiment-8
Aim: Define class with constructer and destructor which display following output:-
Line 1: Object 1 is created.
Line 2: Display function call in main.
Line 3: Object 2 is created.
Line 4: Object 1 is destroyed.
Line 5: Object 2 is destroyed
#include<iostream>
#include<conio.h>
using namespace std;
class truba
{
public:
static int i;
truba()
{
cout<<"\n Object "<<++i<<" is created.";
}
void display()
{
cout<<"\n Display Function Call in main";
}
~truba()
{
cout<<"\n Object "<<i--<<" is destroyed.";
}
};
int truba::i=0;
int main()
{
truba t1;
t1.display();
truba t2;
getch();
return 0;
}
Output:
Experiment-9
Aim: Write a program to Scan mixed character string with space and count capital letter, lower case
letters and Digits. (Use of Loops and Decision statements)
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,vowel=0,upper=0,lower=0,blank=0,special=0;
char st[100];
printf("Enter a string \n");
fgets(st,100,stdin);
for(i=0;i<strlen(st)-1;i++)
{
if(st[i]>=65 && st[i]<=90)
{
upper++;
if(st[i]=='A' || st[i]=='E' || st[i]=='I' || st[i]=='O' || st[i]=='U')
vowel++;
}
else if(st[i]>=97 && st[i]<=122)
{
lower++;
if(st[i]=='a' || st[i]=='e' || st[i]=='i' || st[i]=='o' || st[i]=='u')
vowel++;
}
else if(st[i]==' ')
blank++;
else
special++;
}
printf("The string %s contains :\n",st);
printf("%d vowels \n",vowel);
printf("%d upper case alphabets \n",upper);
printf("%d lower case alphabets \n",lower);
printf("%d blank spaces \n",blank);
printf("%d special characters \n",special);
getch();
return 0;
}
Output:
Experiment-10
Aim: Prepare a class diagram from given detail that includes multiplicity, association relationship,
association end names and association names “School, playground, principal, school board,
classroom, book, student, teacher, cafeteria, restroom, computer, desk, chair, ruler, door, swing”.