def heap_sort(vetor): '''Ordena os elementos do vetor em ordem crescente.''' for i in range(len(vetor) // 2, -1, -1): transforma_em_heap(vetor, i, len(vetor) - 1) for i in range(len(vetor) - 1, 0, -1): troca(vetor, 0, i) transforma_em_heap(vetor, 0, i - 1)
def particiona(vetor): pivo = 0 for i in range(1, len(vetor)): if crescente(vetor[i], vetor[0]): pivo += 1 troca(vetor, pivo, i) troca(vetor, 0, pivo) return pivo
def transforma_em_heap(vetor, inicial, final): '''Transforma o vetor de inteiros em um Heap.''' esquerda = 2 * inicial + 1 direita = esquerda + 1 max = inicial if esquerda <= final and not crescente(vetor[esquerda], vetor[inicial]): max = esquerda if (direita <= final and not crescente(vetor[direita], vetor[max])): max = direita if max != inicial: troca(vetor, inicial, max) transforma_em_heap(vetor, max, final)