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).

```Input
N
x1 x2 ……… xn```
where,
```N is the total numbers to be sorted
xi    input numbers```

Output

y1 y2 ……… yn

where array y is sorted

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

}```

What I have tried:

C#
```void createHeap(struct MinHeap* minHeap)
{
int i,numelems;
// n=heap[0]; //no. of elements
for(i=numelems/2;i>=1;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;
}
}```
Posted
Updated 20-Mar-18 5:10am
v3
OriginalGriff 20-Mar-18 9:08am
And?
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.

## Solution 1

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.