93% found this document useful (15 votes)
16K views2 pages

Optimal MERGE PATTERN

The document describes a program to find the optimal merge pattern for merging sorted arrays. It takes input for number of elements and sorted elements in an array. It then calculates the sum of adjacent elements and stores them in another array to find the minimum path sum. Finally it displays the optimal sums and total external path length.

Uploaded by

anon_389872766
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
93% found this document useful (15 votes)
16K views2 pages

Optimal MERGE PATTERN

The document describes a program to find the optimal merge pattern for merging sorted arrays. It takes input for number of elements and sorted elements in an array. It then calculates the sum of adjacent elements and stores them in another array to find the minimum path sum. Finally it displays the optimal sums and total external path length.

Uploaded by

anon_389872766
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

write aprogram for optimal merge pattern.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,k,a[10],c[10],n,l;
cout<<"Enter the no. of elements\t";
cin>>n;
cout<<"\nEnter the sorted elments for optimal merge pattern";
for(i=0;i<n;i++)
{
cout<<"\t";
cin>>a[i];
}
i=0;k=0;
c[k]=a[i]+a[i+1];
i=2;
while(i<n)
{
k++;
if((c[k-1]+a[i])<=(a[i]+a[i+1]))
{
c[k]=c[k-1]+a[i];
}
else
{
c[k]=a[i]+a[i+1];
i=i+2;
while(i<n)

{ k++;
if((c[k-1]+a[i])<=(c[k-2]+a[i]))
{
c[k]=c[k-1]+a[i];

}
else
{
c[k]=c[k-2]+a[i];

}i++;
}
}i++;
}
k++;
c[k]=c[k-1]+c[k-2];
cout<<"\n\nThe optimal sum are as follows......\n\n";
for(k=0;k<n-1;k++)
{
cout<<c[k]<<"\t";
}
l=0;
for(k=0;k<n-1;k++)
{
l=l+c[k];
}

cout<<"\n\n The external path length is ......"<<l;


getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy