Carregar apresentação
A apresentação está carregando. Por favor, espere
1
Heap Sort TPA – Prof. Mateus Costa
Ordenação Heap Sort TPA – Prof. Mateus Costa
2
Solução da Wikipedia pai = i; void heapsort(char a[], int n)
filho = i*2 + 1; while (filho < n) { if ((filho + 1 < n) && (a[filho + 1] > a[filho])) filho++; if (a[filho] > t) a[pai] = a[filho]; pai = filho; filho = pai*2 + 1; } else break; a[pai] = t; void heapsort(char a[], int n) { int i = n/2, pai, filho; char t; for (;;) if (i > 0) i--; t = a[i]; } else n--; if (n == 0) return; t = a[n]; a[n] = a[0];
3
Minha Versão: Construindo o heap
void constroiHeap(char a[], int esq, int dir) { int i; char aux; i = (esq + dir)/2; while(i >=esq) { if ((a[2*i] < a[2*i+1]) && (2*i+1 <=dir)) troca(&a[2*i],&a[2*i+1]); if (a[i]<a[2*i]) troca(&a[i],&a[2*i]); i--; }
4
Minha Versão: Usando o Heap para ordenar
void meuHeapSort(char a[], int n) { int esq=0,dir=n-1; while(dir>1) { constroiHeap(a,esq,dir); troca(&a[esq],&a[dir]); dir--; }
Apresentações semelhantes
© 2024 SlidePlayer.com.br Inc.
All rights reserved.