Sunteți pe pagina 1din 24

ALGORITMO

HEAPSORT
O PROBLEMA DA ORDENAÇÃO
HEAP? WHAT IS IT?

“Estrutura de dados que enxerga o vetor


como se fosse uma árvore binária.”

4 1 5 6 2 1 2

6 5
MAX E MIN HEAP

MAX-HEAP MIN-HEAP

6 1

5 1 2 6

2 4 4 5
LÓGICA MAX-HEAP

4 1 5 3 2 6

1 5

3 2 6
LÓGICA MAX-HEAP

4 1 5 3 2 6

1 6

3 2 5
LÓGICA MAX-HEAP

4 1 5 3 2 6

3 6

1 2 5
LÓGICA MAX-HEAP

4 1 5 3 2 6

3 4

1 2 5
LÓGICA MAX-HEAP

4 1 5 3 2 6

3 5

1 2 4
LÓGICA MAX-HEAP

6 3 5 1 2 4

3 5

1 2 4
LÓGICA MAX-HEAP

6 3 5 1 2 4

3 5

1 2 4
LÓGICA MAX-HEAP

4 3 5 1 2 6

3 5

1 2 6
LÓGICA MAX-HEAP

5 3 4 1 2 6

3 4

1 2 6
LÓGICA MAX-HEAP

2 3 4 1 5 6

3 4

1 5 6
LÓGICA MAX-HEAP

4 3 2 1 5 6

3 2

1 5 6
LÓGICA MAX-HEAP

1 3 2 4 5 6

3 2

4 5 6
LÓGICA MAX-HEAP

3 1 2 4 5 6

1 2

4 5 6
LÓGICA MAX-HEAP

2 1 3 4 5 6

1 3

4 5 6
LÓGICA MAX-HEAP

1 2 3 4 5 6

2 3

4 5 6
HEAP - CÓDIGO
void constroiHeap( int *p_vetor, int tamanho, int indice_raiz ){

int ramificacao, valor;


valor = p_vetor [ indice_raiz ];

while( indice_raiz <= tamanho/2 ){


ramificacao = 2 * indice_raiz;
if( ramificacao < tamanho && p_vetor[ ramificacao ] < p_vetor
[ramificacao + 1] )
ramificacao++;
if( valor >= p_vetor[ ramificacao ] )//Identifica o max-heap
break;
p_vetor[ indice_raiz ] = p_vetor[ ramificacao ];
indice_raiz = ramificacao;
}
p_vetor[ indice_raiz ] = valor;
}
HEAP - COMPLEXIDADE

“Complexidade O(n log n):


Complexidade encontrada normalmente em
algoritmos de ordenação.”
EQUIPE

Alex Alan
Davi Maia
Elis Ionara
Maxsuella Sousa
Sabrina Oliveira
REFERÊNCIAS

Feofiloff, Paulo - Algoritmos em linguagem C


Szwarcfiter, Jaymer Luiz – Estrutura de dados e seus
algoritmos
Cormen, Thomas H. – Algoritmos: Teoria e Prática

• http://www.ufjf.br/jairo_souza/files/2009/12/2-
Ordena%C3%A7%C3%A3o-HeapSort.pdf
• https://www.youtube.com/watch?v=PfEW37kVyVg
• https://www.geeksforgeeks.org/heap-sort/
• https://www.ime.usp.br/~pf/algoritmos/aulas/hpsrt.html
OBRIGADO PELA ATENÇÃO!

S-ar putea să vă placă și