Question

Write a program for Heap sort. Your program should read from an input file data3.txt and...

Write a program for Heap sort. Your program should read from an input file data3.txt
and write to an output file output3.txt. The input/output format is identical to
programming assignment 1.
For this assignment you cannot use STL. Take the maximum size of array to be 100.
Follow all instructions in programming assignment 1.

Need help code in C++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

void MAX_HEAPIFY(double a[], int i, int n)
{
int l,r,largest;
double loc;
l=2*i;
r=(2*i+1);
if((l<=n)&&a[l]>a[i])
largest=l;
else
largest=i;
if((r<=n)&&(a[r]>a[largest]))
largest=r;
if(largest!=i)
{
loc=a[i];
a[i]=a[largest];
a[largest]=loc;
MAX_HEAPIFY(a, largest,n);
}
}
void BUILD_MAX_HEAP(double a[], int n)
{
for(int k = n/2; k >= 1; k--)
{
MAX_HEAPIFY(a, k, n);
}
}
void HEAPSORT(double a[], int n)
{

BUILD_MAX_HEAP(a,n);
int i;
double temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
MAX_HEAPIFY(a, 1, i - 1);
}
}

int main()
{
int n;
cout<<"Enter the size of the array"<<endl;
cin>>n;
double a[n];
cout<<"Enter the elements in the array"<<endl;
for (int i = 1; i <= n; i++)
{
cin>>a[i];
}
HEAPSORT(a, n);
cout<<":::::::SORTED FORM::::::"<<endl;
for (int i = 1; i <= n; i++)
{
cout<<a[i]<<endl;
}
}

NOTE

input formate

first line of input take size of array n

next n line contain element of array

Add a comment
Know the answer?
Add Answer to:
Write a program for Heap sort. Your program should read from an input file data3.txt and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT