1 / 4

Ordenação

Ordenação. Heap Sort TPA – Prof. Mateus Costa. 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;

clarke-wade
Download Presentation

Ordenação

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ordenação Heap Sort TPA – Prof. Mateus Costa

  2. 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]; } 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; } } Solução da Wikipedia

  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--; } }

More Related