Ordenação
-
Upload
clarke-wade -
Category
Documents
-
view
22 -
download
1
description
Transcript of Ordenação
![Page 1: Ordenação](https://reader035.fdocument.pub/reader035/viewer/2022072013/56812bc9550346895d901d18/html5/thumbnails/1.jpg)
Ordenação
Heap Sort
TPA – Prof. Mateus Costa
![Page 2: Ordenação](https://reader035.fdocument.pub/reader035/viewer/2022072013/56812bc9550346895d901d18/html5/thumbnails/2.jpg)
Solução da Wikipediavoid 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]; }
pai = i; 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; }}
![Page 3: Ordenação](https://reader035.fdocument.pub/reader035/viewer/2022072013/56812bc9550346895d901d18/html5/thumbnails/3.jpg)
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--; } }
![Page 4: Ordenação](https://reader035.fdocument.pub/reader035/viewer/2022072013/56812bc9550346895d901d18/html5/thumbnails/4.jpg)
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--;
}
}