15,958,444 members

See more:

One of the main application of Heaps is Heap-Sort. It consists of 2 steps

(1) Build the heap and

(2) Sorted array is created by repeatedly removing the largest element (Heap is updated after each removal to maintain the heap).

Output

y1 y2 ……… yn

where array y is sorted

Heap sort function is already made available to you

You need to complete the build heap and heapify function

You need to code the following functions

**What I have tried:**

(1) Build the heap and

(2) Sorted array is created by repeatedly removing the largest element (Heap is updated after each removal to maintain the heap).

Input N x1 x2 ……… xnwhere,

N is the total numbers to be sorted xi input numbers

Output

y1 y2 ……… yn

where array y is sorted

Heap sort function is already made available to you

You need to complete the build heap and heapify function

You need to code the following functions

C#

// function creates and build a heap using minHeapify void createHeap(struct MinHeap* minHeap){ // Single Node is a heap // Start from bottom most and rightmost internal node and heapify all internal nodes in bottom up way }

C#

// heapify a min Heap. void heapify(struct MinHeap* minHeap, int idx){ // idx is the index of the node you want to heapify }

C#

void createHeap(struct MinHeap* minHeap) { int i,numelems; // n=heap[0]; //no. of elements for(i=numelems/2;i>=1;i--) down_adjust(minHeap,i); } void heapify(int*a , int len) { int item,i,j,k; for(k=1 ; k<len ; k++) { item = a[k]; i = k; j = (i-1)/2; while( (i>0) && (item>a[j]) ) { a[i] = a[j]; i = j; j = (i-1)/2; } a[i] = item; } }

Comments

I did not look too closely but I saw one obvious problem with the code you posted. In createHeap the for loop starts at numelems/2 but the variable is never set to any value. That's not going to work very well. I suspect that you should pass numelems to the function as a parameter.

Permalink

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

What is the problem?

Where are you stuck?

What help do you need?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.