Tenho que apresentar o HeapSort na faculdade, mais não consegui entende-lo,
o código se encontra em C# mais, esta bem parecido com java, e minha dúvida esta relacionada a lógica
[code]public static class heap{
public static void heapSort(int[] vetor)
{
buildMaxHeap(vetor);
int tamanho = vetor.Length;
for (int i = vetor.Length - 1; i > 0; i--)
{
swap(vetor, i, 0);
maxHeapify(vetor, 0, --tamanho);
}
}
private static void buildMaxHeap(int[] vetor)
{
for (int i = vetor.Length / 2 - 1; i >= 0; i--)
{
maxHeapify(vetor, i, vetor.Length);
Console.WriteLine("Tamanho: " + i);
}
}
private static void maxHeapify(int[] vetor, int pos, int tamanho)
{
int max = 2 * pos + 1, right = max + 1;
if (max < tamanho)
{
if (right < tamanho && vetor[max] < vetor[right])
max = right;
if (vetor[max] > vetor[pos])
{
swap(vetor, max, pos);
maxHeapify(vetor, max, tamanho);
}
}
}
public static void swap(int[] vetor, int j, int aposJ)
{
int aux = vetor[j];
vetor[j] = vetor[aposJ];
vetor[aposJ] = aux;
}
static void Main(string[] args)
{
int i = 0;
int[] vetor = new int[10]{1,2,10,20,4,5,8,6,3,90};
heapSort(vetor);
while(i<10){
Console.WriteLine(vetor[i]);
i++;
}
Console.Read();
}
}[/code]
dúvidas:
1 - maxHeapify, o que é aquela variavel MAX e RIGHT dentro do método, o que aquele if esta comparando ? bem perdido…
desde ja agradeço a ajuda