You are on page 1of 35

Adina Institute of Science & Technology

Department of Computer Science & Engg.


M.Tech CSE-I Sem
Lab File
Advanced Data Structure and Algorithm(MCSE-102)
Object Oriented Technology(MCSE-104)

Submitted To: Submitted By:


INDEX

S.No NAME OF EXPERIMENT DATE OF DATE OF REMARK


PERFORMANCE SUBMISSION

1 Implement Heap Sort for the


following data types int, float, double
using Templates

2 Implement Divide & Conquer


technique to sort the following data
4,62,721,324,426,511,32,61,90
3 Write a program to implement the
following Sorting Techniques
4 Write a program to implement
expression tree traversal
5 Implement any greedy algorithm to
evaluate minimum weight spanning
tree
6 Write a program to implement Stack
with virtual function.

7 Write a program for ‘Plus’ and


‘Minus’ Operator Overloading.
8 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
9 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)
10 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”.
Experiment-1

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

void array :: add ( int item )


{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else cout << "\nArray is full" << endl ;
}

int array :: getcount( )


{
return count ;
}

void array :: quiksort ( int lower, int upper )


{
if ( upper > lower )
{
int i = split ( arr, lower, upper ) ;
quiksort ( lower, i - 1 ) ;
quiksort ( i + 1, upper ) ;
}
}

int array :: split ( int *a, int lower, int upper )


{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}

void array :: display( )


{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}

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

void bubsort :: getdata(){


cout<<"How many elements you require : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
}

void bubsort :: showdata(){


cout<<"\n--Display--\n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}

void bubsort :: sortLogic(){


int temp,exchnge=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
exchnge++;
cout<<"\n arr[j] = "<<arr[j]<<" arr[j+1] = "<<arr[j+1];
}
}
cout<<endl;
if(exchnge==0)
break;
}
}
int main()
{
cout<<"\n*****Bubble Sort*****\n";
bubsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
getch();
}
Output:

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

void Inssort :: getdata(){


cout<<"How many elements you require : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
}

void Inssort :: showdata(){


cout<<"\n--Display--\n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}

void Inssort :: sortLogic()


{
int temp,min,i,j;
for(i=1;i<n;i++)
{
temp = arr[i];
for(j=i;j>0 && arr[j-1]>temp;j--)
{
arr[j] = arr[j-1];
}
arr[j] = temp;
}
}

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

void radixsort :: getdata(){


cout<<"How many elements you require : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
}

void radixsort :: showdata(){


cout<<"\n--Display--\n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}

void radixsort :: sortLogic(){


//for base 10int temp;
int bucket[10][20], buck_count[10], b[10];
int i,j,k,r,no_of_passes=0,divisor=1,largest,pass_no;

largest=arr[0];

for(i=1;i<n;i++) //Find the largest Number


{
if(arr[i] > largest)
largest=arr[i];
}

while(largest > 0) //Find number of digits in largest number


{
no_of_passes++;
largest /= 10;
}

for(pass_no=0; pass_no < no_of_passes; pass_no++)


{

for(k=0; k<10; k++)


buck_count[k]=0; //Initialize bucket count
for(i=0;i<n;i++)
{
r=(arr[i]/divisor) % 10;
bucket[r][buck_count[r]++]=arr[i];
}
i=0; //collect elements from bucket
for(k=0; k<10; k++)
{
for(j=0; j<buck_count[k]; j++)
arr[i++] = bucket[k][j];
}

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 mrg_sort(int arr1[],int n1,int arr2[],int n2,int result[]){


int i,j,k;
i=j=k=0;
while(i< n1 && j<n2){
if(arr1[i] < arr2[j])
result[k++] = arr1[i++];
else if(arr1[i] > arr2[j])
result[k++] = arr2[j++];
else{
result[k++]=arr1[i++];
j++;
}
}
if(i<n1){
while(i<n1)
result[k++]=arr1[i++];
}
else if(j<n2){
while(j<n2)
result[k++]=arr2[j++];
}
return(k);
}

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

int r(char inputchar){ //for checking symbol is operand or operator


if(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/')
return(-1);
else if(inputchar>='a' || inputchar<='z')
return(1);
else if(inputchar>='A' || inputchar<='Z')
return(1);
else
return(-99); //for error
}

//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]);
}

void create_expr_tree(char *suffix){


char symbol;
node *newl,*ptr1,*ptr2;
int flag; //flag=-1 when operator and flag=1 when operand
symbol = suffix[0]; //Read the first symbol from the postfix expr.
for(int i=1;symbol!=0;i++){ //continue till end of the expr.
flag=r(symbol); //check symbol is operand or operator.
if(flag == 1)//if symbol is operand.
{
newl = new node;
newl->data = symbol;
newl->left = NULL;
newl->right = NULL;
push(newl);
}
else{ //If the symbol is operator//pop two top elements.
ptr1=pop();
ptr2=pop();
newl = new node;
newl->data = symbol;
newl->left = ptr2;
newl->right = ptr1;
push(newl);
}
symbol=suffix[i];
}
}

void preOrder(node *tree){


if( tree!=NULL){
cout<<tree->data;
preOrder(tree->left);
preOrder(tree->right);
}
}

void inOrder(node *tree){


if( tree!=NULL){
inOrder( tree->left);
cout<< tree->data;
inOrder(tree->right);
}
}

void postOrder(node *tree){


if( tree!=NULL){
postOrder( tree->left);
postOrder( tree->right);
cout<<tree->data;
}
}
int main(){

cout<<"*****Expression Tree*****\n";
cout<<"Enter Postfix Expression : ";
cin>>postfix;

//Creation of Expression Tree


create_expr_tree(postfix);

//Traversal Operation on expression tree


cout<<"\nIn-Order Traversal : ";
inOrder(arr[0]);

cout<<"\nPre-Order Traversal : ";


preOrder(arr[0]);

cout<<"\nPost-Order Traversal : ";


postOrder(arr[0]);
getch();
}

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

void prims :: createGraph(){


int i,j;
cout<<"Enter Total Nodes : ";
cin>>nodes;
cout<<"\n\nEnter Adjacency Matrix : \n";
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
cin>>graph[i][j];

//Assign infinity to all graph[i][j] where weight is 0.


for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
if(graph[i][j]==0)
graph[i][j]=infi;
}
}
}

void prims :: primsAlgo(){


int selected[ROW],i,j,ne; //ne for no. of edges
int flse=0,tre=1,min,x,y;

for(i=0;i<nodes;i++)
selected[i]=flse;
selected[0]=tre;
ne=0;

while(ne < nodes-1){


min=infi;

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

}
};

class stack : public base


{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty ";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};

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”.

You might also like