Professional Documents
Culture Documents
Algorithms DesignAndAnalysis Heap PDF
Algorithms DesignAndAnalysis Heap PDF
{
//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
}
}