algorithms

64
306 ALGORITHM S Name: V. Sangeetha Roll No: 25369 B. Sc. (H) Computer Science III Semester, 2nd Year ARSD College

Upload: p09arunv

Post on 02-Dec-2014

76 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Algorithms

306

ALGORITHMSName: V. Sangeetha Roll No: 25369

B. Sc. (H) Computer Science III Semester, 2nd Year ARSD College

Page 2: Algorithms
Page 3: Algorithms

INDEX

S.No. Program Remarks

1. WAP to implement Assembly Line Scheduling

2. WAP to find the Longest Common Subsequence

3. WAP to implement Matrix Chain Multiplication

4. WAP to implement the Recursive Activity Selector

5. WAP to implement the Iterative Activity Selector

6. WAP to implement Insertion Sort algorithm

7. WAP to implement Heap Sort algorithm

8. WAP to implement Quick Sort algorithm

9. WAP to implement Randomized Quick Sort algorithm

10. WAP to implement Radix sort algorithm

11. WAP to implement Bucket sort algorithm

12. WAP to implement Counting Sort algorithm

13. WAP to implement Priority Queue (using max_heap)

Page 4: Algorithms
Page 5: Algorithms

PROGRAM 1

Assembly Line Scheduling

//Program to implement Assembly Line Scheduling algorithm

#include<iostream.h>

#include<conio.h>

#define numstn 6

#define numlines 2

void cost(int a[][numstn]);

void trftime(int t[][numstn-1]);

void entry(int e[]);

void exit(int x[]);

void fastestway(int a[][numstn],int t[][numstn-1],int e[],int x[],int n,int f[][numstn],int l[][numstn],int *f2,int *l2);

void printstn(int l[][numstn],int n,int l1);

void main()

{

clrscr();

int f[numlines][numstn];

int l[numlines][numstn];

int a[numlines][numstn];

int t[numlines][numstn-1];

int e[numlines];

int x[numlines];

int n=numstn;

int f1,l1;

Page 6: Algorithms

cost(a);

trftime(t);

entry(e);

exit(x);

fastestway(a,t,e,x,n,f,l,&f1,&l1);

printstn(l,n,l1);

for(int i=0;i<numlines;i++)

{

cout<<"\n";

for(int j=0;j<numstn;j++)

{

cout<<f[i][j]<<"\t";

}

}

cout<<"\n";

for(int p=0;p<numlines;p++)

{

cout<<"\n";

for(int q=0;q<numstn-1;q++)

{

cout<<l[p][q]<<"\t";

}

}

getch();

}

void cost(int a[][numstn])

{

for(int i=0;i<numlines;i++)

Page 7: Algorithms

{

for(int j=0;j<numstn;j++)

{

cout<<"Enter the cost of ("<<i+1<<","<<j+1<<") : ";

cin>>a[i][j];

}

}

}

void trftime(int t[][numstn-1])

{

for(int i=0;i<numlines;i++)

{

for(int j=0;j<numstn-1;j++)

{

if(i==0)

{

cout<<"Enter the transfer time between ("<<i+1<<","<<j+1<<") and ("<<i+2<<","<<j+2<<") : ";

cin>>t[i][j];

}

else

{

cout<<"Enter the transfer time between ("<<i+1<<","<<j+1<<") and ("<<i<<","<<j+2<<") : ";

cin>>t[i][j];

}

}

}

Page 8: Algorithms

}

void entry(int e[])

{

for(int i=0;i<numlines;i++)

{

cout<<"Enter entry time of assembly line "<<i+1<<" : ";

cin>>e[i];

}

}

void exit(int x[])

{

for(int i=0;i<numlines;i++)

{

cout<<"Enter exit time of assembly line "<<i+1<<" : ";

cin>>x[i];

}

}

void fastestway(int a[][numstn],int t[][numstn-1],int e[],int x[],int n,int f[][numstn],int l[][numstn],int *f2,int *l2)

{

f[0][0]=e[0]+a[0][0];

f[1][0]=e[1]+a[1][0];

for(int z=1;z<n;z++)

{

if(f[0][z-1]+a[0][z]<=f[1][z-1]+t[1][z-1]+a[0][z])

{

f[0][z]=f[0][z-1]+a[0][z];

l[0][z-1]=1;

Page 9: Algorithms

}

else

{

f[0][z]=f[1][z-1]+t[1][z-1]+a[0][z];

l[0][z-1]=2;

}

if(f[1][z-1]+a[1][z]<=f[0][z-1]+t[0][z-1]+a[1][z])

{

f[1][z]=f[1][z-1]+a[1][z];

l[1][z-1]=2;

}

else

{

f[1][z]=f[0][z-1]+t[0][z-1]+a[1][z];

l[1][z-1]=1;

}

}

if(f[0][n-1]+x[0]<=f[1][n-1]+x[1])

{

*f2=f[0][n-1]+x[0];

*l2=0;

}

else

{

*f2=f[1][n-1]+x[1];

*l2=1;

}

}

Page 10: Algorithms

void printstn(int l[][numstn],int n,int l1)

{

int i=l1;

cout<<"\nline "<<i+1<<" station "<<n<<endl;

for(int b=n-2;b>=0;b--)

{

i=l[i][b];

cout<<"line "<<i<<" station "<<b+1<<endl;

i=i-1;

}

}

OUTPUT

Enter the cost of (1,1) : 7

Enter the cost of (1,2) : 9

Enter the cost of (1,3) : 3

Enter the cost of (1,4) : 4

Enter the cost of (1,5) : 8

Enter the cost of (1,6) : 4

Enter the cost of (2,1) : 8

Enter the cost of (2,2) : 5

Enter the cost of (2,3) : 6

Enter the cost of (2,4) : 4

Enter the cost of (2,5) : 5

Enter the cost of (2,6) : 7

Page 11: Algorithms

Enter the transfer time between (1,1) and (2,2) : 2

Enter the transfer time between (1,2) and (2,3) : 3

Enter the transfer time between (1,3) and (2,4) : 1

Enter the transfer time between (1,4) and (2,5) : 3

Enter the transfer time between (1,5) and (2,6) : 4

Enter the transfer time between (2,1) and (1,2) : 2

Enter the transfer time between (2,2) and (1,3) : 1

Enter the transfer time between (2,3) and (1,4) : 2

Enter the transfer time between (2,4) and (1,5) : 2

Enter the transfer time between (2,5) and (1,6) : 1

Enter entry time of assembly line 1 : 2

Enter entry time of assembly line 2 : 4

Enter exit time of assembly line 1 : 3

Enter exit time of assembly line 2 : 2

line 1 station 6

line 2 station 5

line 2 station 4

line 1 station 3

line 2 station 2

line 1 station 1

9 18 20 24 32 35

12 16 22 25 30 37

1 2 1 1 2

1 2 1 2 2

Page 12: Algorithms

PROGRAM 2

Longest Common Subsequence

//Program to implement longest common subsequence algorithm

#include<iostream.h>

#include<conio.h>

#include<string.h>

void print_lcs(char b[20][20],char x[],int i,int j);

void lcs(char x[],char y[])

{

int m=strlen(x);

int n=strlen(y);

char c[20][20];

char b[20][20];

for(int i=1;i<=m;i++)

c[i][0]=0;

for(int j=0;j<=m;j++)

c[0][j]=0;

for(i=1;i<=m;i++)

{

for(j=1;j<=n;j++)

{

if(x[i]==y[i])

{

c[i][j]=c[i-1][j-1]+1;

Page 13: Algorithms

b[i][j]='/';

}

else if(c[i-1][j]>=c[i][j-1])

{

c[i][j]=c[i-1][j];

b[i][j]='|';

}

else

{

c[i][j]=c[i][j-1];

b[i][j]='-';

}

}

}

cout<<"\n The longest common sequence found is: "<<endl;

print_lcs(b,x,m,n);

}

void print_lcs(char b[20][20], char x[], int i, int j)

{

if((i==0)||(j==0))

return;

if(b[i][j]=='/')

{

print_lcs(b,x,i-1,j-1);

cout<<x[i]<<" ";

}

else if(b[i][j]=='|')

Page 14: Algorithms

print_lcs(b,x,i-1,j);

else

print_lcs(b,x,i,j-1);

}

void main()

{

clrscr();

char x[20],X[20],y[20],Y[20];

cout<<"\n Enter first sequence\t"<<endl;

cin>>x;

cout<<"\n Enter second sequence: "<<endl;

cin>>y;

for(int i=0;i<strlen(x);i++)

X[i+1]=x[i];

for(i=0;i<strlen(y);i++)

Y[i+1]=y[i];

lcs(X,Y);

getch();

}

Page 15: Algorithms

OUTPUT

Enter first sequence

abscf

Enter second sequence:

abodrsc

The longest common sequence found is:

absc

Page 16: Algorithms

PROGRAM 3

Matrix Chain Multiplication

//Program to implement matrix chain mutiplication algorithm

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[20][20],p[10],n,i,j,m[20][20],q;

cout<<"Enter the no. of matrices: "<<endl;

cin>>n;

for(i=1;i<=n;i++)

{

cout<<"Enter the dimension of "<<i<<"matrix"<<endl;

for(j=1;j<=2;j++)

{

cin>>a[i][j];

}

}

for(i=1;i<n+1;i++)

{

p[0]=a[1][1];

p[i]=a[i][2];

}

Page 17: Algorithms

for(i=1;i<=n;i++)

{

m[i][i]=0;

}

for(int l=2;l<=n;l++)

{

for(i=1;i<=n-l+1;i++)

{

j=i+ l-1;

m[i][j]=175751;

for(int k=i;k<=j-1;k++)

{

q=m[i][k]+m[k+1][j]+(p[i-1]*p[k]*p[j]);

if(q<m[i][j] && q>0)

{

m[i][j]=q;

}

}

}

}

cout<<"The matrix is:\n"<<endl;

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

Page 18: Algorithms

if(i<=j)

cout<<m[i][j]<<" ";

}

cout<<endl;

}

getch();

}

OUTPUT

Enter the no. of matrices:

5

Enter the dimension of 1matrix

2

3

Enter the dimension of 2matrix

3

4

Enter the dimension of 3matrix

4

5

Enter the dimension of 4matrix

5

6

Enter the dimension of 5matrix

Page 19: Algorithms

6

7

The matrix is:

0 24 64 124 208

0 60 150 276

0 120 288

0 210

0

Page 20: Algorithms

PROGRAM 4

Recursive Activity Selector

//Program to implement Recursive activity selector algorithm

#include<iostream.h>

#include<conio.h>

#define MAX 10000

void starting_time(int n, int s[100]);

void finishing_time(int n, int f[100]);

void recursive_activity_selector(int s[100], int f[100], int i, int j);

void main()

{

clrscr();

int n, s[100], f[100];

cout<<"\n\n Enter number of activities : ";

cin>>n;

starting_time(n, s);

finishing_time(n, f);

recursive_activity_selector(s, f, 0, n+1);

getch();

}

void starting_time(int n, int s[100])

{

Page 21: Algorithms

cout<<"\n\n Enter starting times of activities : ";

s[0]=0;

for(int i=1; i<=n; i++)

cin>>s[i];

s[n+1]=MAX;

}

void finishing_time(int n, int f[100])

{

cout<<"\n\n Enter finishing times of activities : ";

f[0]=0;

for(int i=1; i<=n; i++)

cin>>f[i];

f[n+1]=MAX;

cout<<"\n\n Compatible activities are : \n";

}

void recursive_activity_selector(int s[100], int f[100], int i, int j)

{

int m=i+1;

while((m<j)&&(s[m]<f[i]))

m=m+1;

if(m<j)

{

cout<<"A["<<m<<"]\t";

recursive_activity_selector(s, f, m, j);

}

}

Page 22: Algorithms

OUTPUT

Enter number of activities : 11 Enter starting times of activities : 1 3 0 5 3 5 6 8 8 2 12 Enter finishing times of activities : 4 5 6 7 8 9 10 11 12 13 14 Compatible activities are :

A[1] A[4] A[8] A[11]

Page 23: Algorithms

PROGRAM 5

Iterative Activity Selector

//Program to implement Iterative activity selector algorithm

#include<iostream.h>

#include<conio.h>

#define MAX 10000

void starting_time(int n, int s[100]);

void finishing_time(int n, int f[100]);

void iterative_activity_selector(int s[100], int f[100], int n);

void main()

{

clrscr();

int n, s[100], f[100];

cout<<"\n\n Enter number of activities : ";

cin>>n;

starting_time(n, s);

finishing_time(n, f);

iterative_activity_selector(s, f, n);

getch();

}

void starting_time(int n, int s[100])

{

Page 24: Algorithms

cout<<"\n\n Enter starting times of activities : ";

s[0]=0;

for(int i=1; i<=n; i++)

cin>>s[i];

s[n+1]=MAX;

}

void finishing_time(int n, int f[100])

{

cout<<"\n\n Enter finishing times of activities : ";

f[0]=0;

for(int i=1; i<=n; i++)

cin>>f[i];

f[n+1]=MAX;

}

void iterative_activity_selector(int s[100], int f[100], int n)

{

cout<<"\n\n Compatible activities are : \n";

int i=1;

cout<<"\n A["<<i<<"]\t";

for(int m=2; m<=n; m++)

{

if(s[m]>=f[i])

{

cout<<"A["<<m<<"]\t";

i=m;

}

Page 25: Algorithms

}

}

OUTPUT

Enter number of activities : 11 Enter starting times of activities : 1 3 0 5 3 5 6 8 8 2 12 Enter finishing times of activities : 4 5 6 7 8 9 10 11 12 13 14 Compatible activities are : A[1] A[4] A[8] A[11]

Page 26: Algorithms

PROGRAM 6

Insertion Sort

//Program to implement insertion sort algorithm

#include<iostream.h>

#include<conio.h>

void input(int arr[50],int sz)

{

for(int i=0;i<sz;i++)

{

cout<<"Enter element "<<(i+1)<<" : ";

cin>>arr[i];

}

}

void insertion(int arr[50],int sz)

{

int temp;

for(int i=1;i<sz;i++)

{

for(int j=0;j<i;j++)

{

if(arr[j]>arr[i])

{

temp=arr[j];

arr[j]=arr[i];

for(int k=i;k>j;k--)

Page 27: Algorithms

arr[k]=arr[k-1];

arr[k+1]=temp;

}

}

}

}

void display(int arr[50],int sz)

{

cout<<"Sorted Array :\n";

for(int i=0;i<sz;i++)

cout<<arr[i]<<" ";

}

void main()

{

clrscr();

int arr[50],size;

cout<<"Enter the size of the array : ";

cin>>size;

input(arr,size);

insertion(arr,size);

display(arr,size);

getch();

}

Page 28: Algorithms

OUTPUT

Enter the size of the array : 7

Enter element 1 : 0

Enter element 2 : 4

Enter element 3 : 2

Enter element 4 : 7

Enter element 5 : 8

Enter element 6 : 3

Enter element 7 : 4

Sorted Array :

0 2 3 4 4 7 8

Page 29: Algorithms

PROGRAM 7

Heap Sort

//Program to implement Heapsort algorithm

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void maxheapify(int *a,int i);

void buildmaxheap(int *a);

void heapsort(int *a);

int n,size;

void main()

{

clrscr();

int a[100];

cout<<"Enter the size of the heap : "<<endl;

cin>>n;

size=n;

cout<<"Enter the values of the heap : "<<endl;

Page 30: Algorithms

for(int i=1;i<=n;i++)

cin>>a[i];

heapsort(a);

getch();

}

void buildmaxheap(int *a)

{

for(int i=n/2;i>=1;i--)

{

maxheapify(a,i);

}

}

void maxheapify(int *a,int i)

{

int l=2*i;

int r=2*i+1;

int largest;

if(l<=n && a[l]>a[i])

largest=l;

Page 31: Algorithms

else

largest=i;

if(r<=n && a[r]>a[largest])

largest=r;

if(largest!=i)

{

int temp=a[i];

a[i]=a[largest];

a[largest]=temp;

maxheapify(a,largest);

}

}

void heapsort(int *a)

{

buildmaxheap(a);

for(int i=n;i>=2;i--)

{

int temp=a[1];

a[1]=a[i];

a[i]=temp;

n=n-1;

maxheapify(a,1);

}

cout<<"The sorted array is :"<<endl;

Page 32: Algorithms

for(i=1;i<=size;i++)

cout<<a[i]<<" ";

}

OUTPUT

Enter the size of the heap :

6

Enter the values of the heap :

31

72

50

19

12

3

the sorted array is :

3 12 19 31 50 72

Page 33: Algorithms

PROGRAM 8

Quick Sort

//Program to implement Quicksort algorithm

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void quick(int *,int p,int r);

int partition(int *,int p,int r);

void main()

{

clrscr();

int a[100],s;

cout<<"Enter the no of elements of the array : "<<endl;

cin>>s;

cout<<"Enter the values"<<endl;

for(int i=0;i<s;i++)

cin>>a[i];

quick(a,0,s-1);

cout<<"The sorted array is"<<endl;

for(i=0;i<s;i++)

cout<<a[i]<<" ";

getch();

Page 34: Algorithms

}

void quick(int *a,int p,int r)

{

if(p<r)

{

int q=partition(a,p,r);

quick(a,p,q-1);

quick(a,q+1,r);

}

}

int partition(int *a,int p,int r)

{

int x=a[r],temp;

int i=p-1;

for(int j=p;j<=r-1;j++)

{

if(a[j]<=x)

{

i=i+1;

temp=a[i];

a[i]=a[j];

a[j]=temp;

Page 35: Algorithms

}

}

temp=a[i+1];

a[i+1]=a[r];

a[r]=temp;

return i+1;

}

OUTPUT

Enter the no of elements of the array :

6

Enter the values

23

12

10

15

56

36

The sorted array is

10 12 15 23 36 56

Page 36: Algorithms

PROGRAM 9

Randomized Quick Sort

//Program to implement randomized quicksort algorithm

#include<stdio.h>

#include<stdlib.h>

#include<iostream.h>

#include<conio.h>

int partition(int a[],int l,int r)

{

int temp;

int i=l-1;

int j;

randomize();

int p=random(r-l);

p=p+l;

temp=a[p];

a[p]=a[r];

a[r]=temp;

int v=a[r];

for(j=l;j<=r-1;++j)

{

if(a[j]<=v)

{

i=i+1;

temp=a[i];

Page 37: Algorithms

a[i]=a[j];

a[j]=temp;

}

}

temp=a[i+1];

a[i+1]=a[r];

a[r]=temp;

return i+1;

}

void quicksort(int a[],int l,int r)

{

if(l<r)

{

int i=partition(a,l,r);

quicksort(a,l,i-1);

quicksort(a,i+1,r);

}

}

void main()

{

clrscr();

int a[20],n,i;

for(i=1;i<=20;++i)

a[i]=0;

cout<<"\nEnter the no.of elements";

cin>>n;

cout<<"\nEnter the elements";

for(i=1;i<=n;++i)

Page 38: Algorithms

cin>>a[i];

cout<<"\nSorted array is:\n";

quicksort(a,1,n);

for(i=1;i<=n;++i)

cout<<a[i]<<” “;

getch();

}

OUTPUT

Enter the no of elements5

Enter the elements6

4

2

7

9

Sorted array is:

2 4 6 7 9

Page 39: Algorithms

PROGRAM 10

Radix Sort

//Program to implement Radix sort algorithm

#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<stdio.h>

void radixsort(int x[],int n,int m)

{

int i,j,k,c,d,flag,exp;

for(i=1;i<=m;i++)

{

exp=pow(10,i-1);

for(j=0;j<n-1;j++)

{

for(k=0;k<n-j-1;k++)

{

c=(x[k]/exp)%10;

d=(x[k+1]/exp)%10;

if(c>d)

{

flag=x[k];

x[k]=x[k+1];

x[k+1]=flag;

Page 40: Algorithms

}

}

}

}

}

void main()

{

clrscr();

int a[20],n,s,i;

cout<<"Enter the no. of digits: "<<endl;

cin>>s;

cout<<"Enter the no. of elements in the array: "<<endl;

cin>>n;

cout<<"Enter the elements: "<<endl;

for(i=0;i<n;i++)

cin>>a[i];

radixsort(a,n,s);

cout<<"The sorted order: "<<endl;

for(i=0;i<n;i++)

cout<<a[i]<<"\n";

getch();

}

Page 41: Algorithms

OUTPUT

Enter the no of elements of the array :

5

Enter the no of digits of the nos :

3

Enter the elements of the array.

450

230

540

125

100

The sorted array is

100 125 230 450 540

Page 42: Algorithms

PROGRAM 11

Bucket Sort

//Program to implement Bucket sort algorithm

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class node

{

private:

int count;

float num;

node *ptr,*first;

public:

node();

void create(float a);

void display();

};

node::node()

{

first=NULL;

}

void node::create(float a)

{

node *head,*temp,*x;

Page 43: Algorithms

head=new node;

head->num=a;

if(first==NULL)

{

head->ptr=NULL;

first=temp=head;

}

else

{

x=temp=first;

while(temp->num<a && temp!=NULL)

{

x=temp;

temp=temp->ptr;

}

if(temp==first)

{

head->ptr=first;

first=head;

}

else

{

x->ptr=head;

head->ptr=temp;

Page 44: Algorithms

}

}

count++;

}

void node::display()

{

node *temp;

temp=first;

while(temp!=NULL)

{

cout<<" "<<temp->num;

temp=temp->ptr;

}

}

void main()

{

clrscr();

float a[100];

int n;

cout<<" Enter the no of elements : ";

cin>>n;

node z[10];

cout<<"enter the elements........"<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

for(int g=0;g<10;g++)

Page 45: Algorithms

{

float d=(float)g/10;

for(int j=0;j<n;j++)

{

if(a[j]>=d && a[j]<(float)d+0.1)

{

z[g].create(a[j]);

}

}

}

cout<<"the sorted array is......"<<endl;

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

z[i].display();

getch();

}

OUTPUT

Enter the no of elements :

5

Enter the elements

0.54

0.23

0.54

0.12

0.10

The sorted array is

0.1 0.12 0.23 0.54 0.54

Page 46: Algorithms

PROGRAM 12

Counting Sort

//Program to implement Counting sort algorithm

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void main()

{

clrscr();

int a[100],b[100],c[100];

int n,max,i,j,k,count=0;

cout<<"Enter the no of elements of the array : "<<endl;

cin>>n;

cout<<"Enter the nos."<<endl;

for(i=1;i<=n;i++)

cin>>a[i];

cout<<"Enter the max no: "<<endl;

cin>>max;

for(i=0;i<=max;i++)

{

count=0;

for(j=1;j<=n;j++)

{

if(i==a[j])

count++;

Page 47: Algorithms

}

b[i]=count;

}

for(i=1;i<=max;i++)

b[i]=b[i-1]+b[i];

for(i=n;i>0;i--)

{

for(j=0;j<=max;j++)

{

if(a[i]==j)

{

c[b[j]]=j;

b[j]--;

}

}

}

cout<<"The sorted array is......."<<endl;

for(i=1;i<=n;i++)

cout<<c[i]<<" ";

getch();

}

Page 48: Algorithms

OUTPUT

Enter the no of elements of the array :

5

Enter the nos

2

6

4

7

9

Enter the max no:

9

The sorted array is

2 4 6 7 9

Page 49: Algorithms

PROGRAM 13

Priority Queue

//Program to implement priority queues (using max heap)

#include<iostream.h>

#include<conio.h>

class priority

{

int heapsize, largest, a[50], count;

public:

priority()

{

count=0;

heapsize=0;

}

void swap(int &a, int &b);

int heap_extract_max();

int parent(int);

int left(int);

int right(int);

int heap_max();

void heapify(int);

void heap_increase_key(int, int);

void max_heap_insert(int);

void getarray();

Page 50: Algorithms

void getcount();

};

int priority::heap_extract_max()

{

if(heapsize<1)

{

cout<<"\n\n Heap Underflow!!!";

return 0;

}

else

{

int max=a[1];

a[1]=a[heapsize];

heapsize-=1;

heapify(1);

return max;

}

}

void priority::heapify(int i)

{

int l=left(i);

int r=right(i);

int largest=i;

if(l<=heapsize)

{

count++;

Page 51: Algorithms

if(a[l]>a[i])

largest=l;

else

largest=i;

}

if(r<=heapsize)

{

count++;

if(a[r]>a[largest])

largest=r;

}

if(largest!=i)

{

swap(a[largest], a[i]);

heapify(largest);

}

}

void priority::swap(int &a, int &b)

{

int temp=0;

temp=a;

a=b;

b=temp;

}

void priority::heap_increase_key(int i, int key)

{

Page 52: Algorithms

if(key<a[i])

{

cout<<"\n\n New key smaller than current key!!!";

count++;

}

else

{

a[i]=key;

while((i>1)&&(a[parent(i)]<a[i]))

{

count++;

int temp=a[i];

a[i]=a[parent(i)];

a[parent(i)]=temp;

i=parent(i);

}

}

}

void priority::max_heap_insert(int key)

{

heapsize++;

a[heapsize]=-3000;

heap_increase_key(heapsize, key);

}

void priority::getarray()

{

Page 53: Algorithms

cout<<"\n\n The resultant array is : ";

for(int i=1; i<=heapsize; i++)

cout<<" "<<a[i];

}

void priority::getcount()

{

cout<<"\n\n The number of comparisons are : "<<count;

}

int priority::parent(int i)

{

return (i/2);

}

int priority::left(int i)

{

return (2*i);

}

int priority::right(int i)

{

return (2*i+1);

}

void main()

{

clrscr();

int num;

int ch;

char ch1;

Page 54: Algorithms

priority ob;

do

{

cout<<"\n\n 1. Insert an element"

<<"\n 2. Display array"

<<"\n 3. Extract maximum element"

<<"\n\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1 : cout<<"\n\n Enter the value : ";

cin>>num;

ob.max_heap_insert(num);

break;

case 2 : ob.getarray();

ob.getcount();

break;

case 3 : cout<<"Maximum element in array is : "<<ob.heap_extract_max();

break;

default : cout<<"\n\n Wrong choice!! Try again...!";

}

cout<<"\n\n Do you want to continue?";

cin>>ch1;

}while((ch1=='y')||(ch1=='Y'));

getch();

}

Page 55: Algorithms

OUTPUT

1. Insert an element

2. Display array

3. Extract maximum element

Enter your choice : 1

Enter the value : 5

Do you want to continue?y

1. Insert an element

2. Display array

3. Extract maximum element

Enter your choice : 1

Enter the value : 6

Do you want to continue?y

Insert an element

Display array

Extract maximum element

Page 56: Algorithms

Enter your choice : 1

Enter the value : 7

Do you want to continue?y

Insert an element

Display array

Extract maximum element

Enter your choice : 2

The resultant array is : 7 5 6 The number of comparisons are : 2

Do you want to continue?y

Insert an element

Display array

Extract maximum element

Enter your choice : 3

Maximum element in array is : 7

Do you want to continue?y

Insert an element

Display array

Page 57: Algorithms

Extract maximum element

Enter your choice : 2

The resultant array is : 6 5

The number of comparisons are : 3

Do you want to continue?n