int[]H;
int size;
public void constructHeap()
{
int parent, v, child;
boolean heap;
for(int i = size/2; i >= 1; i--)//visit each parent node
{
parent = i;
v = H[parent];
heap = false;
while(!heap && parent * 2 <= size) { child = parent *2; if(child <> H[child])
child++;
if(v >= H[child])
heap = true;
else
{
H[parent] = H[child];
parent = child;
H[parent] = v;
}
}
}
}
public void maxRootDeletion()
{
int lastElement;
for (int i = size; i >=1; i--)
{
lastElement = H[size];
H[size--]= H[1];
H[1] = lastElement;
constructHeap();
}
}
public void heapSort()
{
constructHeap();
maxRootDeletion();
}
No hay comentarios:
Publicar un comentario