Question

1. (10 points) Write a program to implement Heapsort. The input should be an array of size at least 15. Have the user enter tHEAPSORT (A) BUILD-MAX-HEAP (A) for i = A.length down to 2 swap A[1] with A[i] A.heap-size = A.heap-size-1 MAX-HEAPIFY (A, 1)

How do I write this in the c++ language?

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

I HAVE IMPLEMENTED THE ABOVE ALGORITHMS TO SORT THE ARRAY USING HEAP.

I HAVE PASTED THE CODE AND OUTPUT SCREENSHOT.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int heap_size=8;
int sort_count=0;
int parent(int i)
{
   return (floor(i));

}
int left(int i)
{
   return 2*i;
}
int right(int i)
{
   return (2*i+1);
}
void printarray(int *a,int n)
{
   int i=0;
   //cout<<"\n\t....THE SORTED ARRAY IS\t ";
   cout<<"INDEX"<<endl;
   for(i=1;i<=n;i++)
       cout<<i<<"\t";
   cout<<endl<<"ELEMENTS:"<<endl;
   for(i=1;i<=n;i++)
       cout<<a[i]<<"\t";
   cout<<endl;
  
   cout<<"*******************************************************************"<<endl;

}
void MAX_HEAPIFY(int *a,int i)
{
   int l,r,largest,temp;
   l=left(i);
   r=right(i);
   if(l<=heap_size && a[l]>a[i] )
       largest=l;
   else
       largest=i;
   if(r<=heap_size && a[r]>a[largest])
       largest=r;
   if(largest!=i)
   {
       temp=a[i];
       a[i]=a[largest];
       a[largest]=temp;
       MAX_HEAPIFY(a,largest);
   }
   sort_count++;
   printarray(a,i);
}
void BUILD_MAX_HEAP(int *a,int length)
{
   int i=0;
   for(i=floor(length/2);i>=1;i--)
       MAX_HEAPIFY(a,i);
}
void HEAPSORT(int *a,int length)
{
   int i=0,temp=0;
   BUILD_MAX_HEAP(a,length);
   for(i=length;i>=1;i--)
   {
       temp=a[i];
       a[i]=a[1];
       a[1]=temp;
       heap_size--;
       MAX_HEAPIFY(a,1);
      
   }
}
void readarray(int *a,int l)
{
   int i;
   cout<<"\n....Enter "<<l<<" values ";
   for(i=1;i<=l;i++)
       cin>>a[i];
}

int main()
{
   int *a;
   int length=8;
   cout<<"******************************************"<<endl;
   cout<<"* HEAP SORT PROGRAM * "<<endl;
   cout<<"******************************************"<<endl;
   cout<<"\n....Enter number of values \n\t";
   cin>>length;
   heap_size=length;
   a=(int*)malloc(sizeof(int)*length);
   readarray(a,length);
   cout<<"******************************************"<<endl;
   cout<<"* THE GIVEN ARRAY IS * "<<endl;
   cout<<"******************************************"<<endl;
   printarray(a,length);
   cout<<"******************************************"<<endl;
   cout<<"* THE INTERMENDIATE MAX HEAP *"<<endl;
   cout<<"******************************************"<<endl;
   HEAPSORT(a,length);
   //printarray(a,length);
   cout<<"******************************************"<<endl;
   cout<<"* THE SORTED ARRAY IS *"<<endl;
   cout<<"******************************************"<<endl;
   printarray(a,length);
   cout<<"******************************************"<<endl;
   cout<<"* THE WORST CASE COMPLEXITY IS *"<<endl;
   cout<<"******************************************"<<endl;
   cout<<"\tNumber of elements:"<<length<<"\n O(nln(n)): "<<(sort_count-length);
}

OUTPUT:

CAV Command Prompt D:\My DS LAB\heap>g++ HEAP_SORT_WITH_TIMECOMPLEXITY.cpp D:\My DS LAB\heapa HEAP SORT PROGRAM .... Enter nu

1 2 INDEX ELEMENTS: INDEX 1 2 ELEMENTS: 21 INDEX ELEMENTS: INDEX ELEMENTS: INDEX ELEMENTS: 家家家家家家家家家家家家家家家家家家家家家家家家家家家家家家家家家家

Complexity Analysis:

Since we are building a heap in tree fashion the number for comparisions for each element With respect to its parent is ln(n).

we nedd to compare n elements with its parent so ,

the time complexity is O(n ln (n))

for example consider n =15

ln(15)=2.70805

=>O(nln( n))=15*2.708.5=40.62 (which is an approimation of above result).

Add a comment
Know the answer?
Add Answer to:
How do I write this in the c++ language? 1. (10 points) Write a program to...
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
  • Please ignore red marks. Thanks 6. (8 pts) Illustrate the algorithmic operations on the maximum binary...

    Please ignore red marks. Thanks 6. (8 pts) Illustrate the algorithmic operations on the maximum binary heap data sti 'perations on the maximum binary heap data structure as directed. BUILD-MAX-HEAP(A) MAX-HEAPIFY (A. i) 1 A heap-size = A.length 11 = LEFT() 2 for i = A.length/2) downto 1 2 r = RIGHT() 3 MAX-HEAPIFY (A,i) 3 if / S 4.heap-size and All > A[i] HEAP-EXTRACT-MAX (A) 4 largest = 1 5 else largest = 1 1 if A.heap-size <1 6...

  • #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...

    #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...

  • Algorithm Please answer Number 6 right and clearly! Thanks a lot For Problems #4 through #6,...

    Algorithm Please answer Number 6 right and clearly! Thanks a lot For Problems #4 through #6, consider the following pseudocode (ref: CLRSpp. 154-160): 01 HEAP SORT (A) 02 BUILD-MAX-HEAP (A) 03 for i A. length downto 2 04 exchange A [1] and Ali 05 A heapsize A heapsize 1 06 MAX-HEAPIEY (A, 1) 07 BUILD-MAX-HEAP (A) 08 A heapsize A. length 09 for i floor (A length/2) down to 1 10 MAX-HEAPIEY (A ,i) 11 MAX-HEAPIFY (A,i) 12 L 2*i...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Write a program in either C++ or Java meeting these requirements Description: In this program, we...

    Write a program in either C++ or Java meeting these requirements Description: In this program, we assume that our data comes in dynamically. We need to maintain our data in a heap, after every insertion and deletion. We also need to handle the underlying array dynamically. whenever we detect an overflow in our array, we will create a new array with size doubling the previous size. Requirements: 1. (Dynamic Data) when we generate the data, we simulate dynamic data. We...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • In C++ In this assignment you will implement a heap data structure that is able to do Heapsort. ...

    in C++ In this assignment you will implement a heap data structure that is able to do Heapsort. Implement it in an array. The first line of input will be given in the form of a list of numbers to insert into a binary tree in level order. Assume it is a complete tree. When finished inserting heapify the tree into a MAXHEAP. Print out the level order traversal for it. Then remove the head (min) and print it out...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • add/ remove any methods to the program. please post new code and output Min Heap: public...

    add/ remove any methods to the program. please post new code and output Min Heap: public class MinHeap { private int[] Heap; private int size; private int maxsize; private static final int FRONT = 1; public MinHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } private int parent(int pos) { return pos / 2; } private int leftChild(int pos) { return (2 * pos); } private int rightChild(int pos) {...

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
ADVERTISEMENT