You are on page 1of 4

insert (Heap h, int x)

{
//Heap is stored in an array
if (heap is empty)
{
h[0]=x;
}
else
{
// the current element is to be placed at position n
if(x<h[n/2])
{
h[n]=x;
}
else
{
h[n]=x;
while (h[n]<h[n/2])
{
swap (h[n], h[n/2]);
}
}
}
}

n
O
{
heap h=heapify (elements);

i=0;
while(i!=n)
{
x=delete(
//the delete function removes the element at the root of the heap and inserts
it into x
insert(sorted_list, x);
//the element x is inserted into sorted_list
}
}

You might also like