Sunday, 7 April 2013

MERGE TWO GIVEN HEAPS



#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],b[100],c[100],i,j,n1,n2,n,s,s1,ns,temp=0,mk;
clrscr();
printf("--------- MERGEING TWO HEAP ARRAY ----------\n\n");
printf("Size of Array A:");
scanf("%d",&n1);
printf("\nEneter the A array values:");
for(i=0;i<n1;i++)
{
scanf("%d",&a[i]);
}
printf("\n\n Size of Array B:");
scanf("%d",&n2);
printf("\n Eneter the B array value:");
for(i=0;i<n2;i++)
{
scanf("%d",&b[i]);
}
j=0;
n=n1+n2;
for(i=0;i<n;i++)
{
if(i<n1)
{
c[i]=a[i];
}
else
{
c[i]=b[j];
j++;
}
}
printf("\n-----------MERGEING TWO HEAP ARRAY----------\n\n");
for(i=0;i<n;i++)
{
printf("%d\t",c[i]);
}
printf("\n\n---------AFTER SORTING AND DELETE DUPLICATE HEAP ARRAY --------\n\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(c[i]==c[j])
{
for(s=j;s<n;s++)
{
c[s]=c[s+1];
}
n=n-1;
}
}
}

for(i=0;i<n;i++)
{
for(mk=0;mk<n;mk++)
{
if(c[i]<c[mk])
{
temp=c[i];
c[i]=c[mk];
c[mk]=temp;
}
}
}
for(i=0;i<n;i++){
printf("%d\t",c[i]);}
getch();
}
OUTPUT


--------- MERGEING TWO HEAP ARRAY ----------

Size of Array A:3

Eneter the A array values:5
3
7

 Size of Array B:3

 Eneter the B array value:4
7
5
-----------MERGEING TWO HEAP ARRAY----------

5       3       7       4       7       5

---------AFTER SORTING AND DELETE DUPLICATE HEAP ARRAY --------

3       4       5       7

No comments:

Post a Comment