answersLogoWhite

0

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

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
More answers

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

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: C program for optimal merge pattern?
Write your answer...
Submit
Still have questions?
magnify glass
imp